file_exists ( string filename)

解决方案 »

  1.   

    摘自4images里面的一个函数:
    function remote_file_exists($url) { // similar to file_exists(), checks existence of remote files
      $url = trim($url);
      if (!preg_match("=://=", $url)) $url = "http://$url";
      if (!($url = @parse_url($url))) {
        return false;
      }
      if (!eregi("http", $url['scheme'])) {
        return false;
      }
      $url['port'] = (!isset($url['port'])) ? 80 : $url['port'];
      $url['path'] = (!isset($url['path'])) ? "/" : $url['path'];
      $fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
      if (!$fp) {
        return false;
      }
      else {
        $head = "";
        $httpRequest = "HEAD ".$url['path']." HTTP/1.1\r\n"
                      ."HOST: ".$url['host']."\r\n"
                      ."Connection: close\r\n\r\n";
        fputs($fp, $httpRequest);
        while (!feof($fp)) {
          $head .= fgets($fp, 1024);
        }
        fclose($fp);    preg_match("=^(HTTP/\d+\.\d+) (\d{3}) ([^\r\n]*)=", $head, $matches);
        if ($matches[2] == 200) {
          return true;
        }
      }
    }