<?php
$url = 'http://59.64.*.*/2.txt';
/* 
  函数:remote_file_exists 
  功能:判断远程文件是否存在 
  参数: $url_file -远程文件URL 
  返回:存在返回true,不存在或者其他原因返回false 
  */ 
if(remote_file_exists($url)){
echo "23";
}else{
echo "12";
}
  function remote_file_exists($url_file){ 
  //检测输入 
  $url_file = trim($url_file); 
  if (empty($url_file)) { return false; } 
  $url_arr = parse_url($url_file); 
  if (!is_array($url_arr) || empty($url_arr)){return false; } 
  //获取请求数据 
  $host = $url_arr['host']; 
  $path = $url_arr['path'] ."?".$url_arr['query']; 
  $port = isset($url_arr['port']) ?$url_arr['port'] : "80"; 
  //连接服务器 
  $fp = fsockopen($host, $port, $err_no, $err_str,30); 
  if (!$fp){ return false; } 
  //构造请求协议 
  $request_str = "GET ".$path."HTTP/1.1\r\n"; 
  $request_str .= "Host:".$host."\r\n"; 
  $request_str .= "Connection:Close\r\n\r\n"; 
  //发送请求 
  fwrite($fp,$request_str); 
  $first_header = fgets($fp, 1024); 
  fclose($fp); 
  //判断文件是否存在 
  if (trim($first_header) == ""){ return false;} 
  if (!preg_match("/200/", $first_header)){ 
  return false; 
  } 
  return true; 
  } 
?> 为什么报错呢?Parse error: syntax error, unexpected T_STRING in D:\Apache Group\Apache2\htdocs\play\exist.php on line 22

解决方案 »

  1.   

    php5自带的 file_exists 支持url,
    直接用就行了
      

  2.   

    file_exists不能用于远程文件吧?and另外的错误来了,
    Fatal error: Call to undefined function   fwrite() in D:\Apache Group\Apache2\htdocs\play\exist.php on line 52太诡异了
      

  3.   


    fopen肯定可以,试试看var_dump(fopen("http://www.baidu.com/img/baidu_logo.gif", "r"));
      

  4.   

    file_exists 并不是完全不能用于远程文件,而是要求这个远程文件的wrapper必须支持 stat 系列函数由于http协议不支持这个函数,所以不行,ftp是可以的<?php
        clearstatcache();
        var_dump(file_exists("ftp://ftp.debian.org/debian/"));
    ?>
      

  5.   


    提供给客户端下载,非常感谢回答啦!fopen还没搞定的说,呵呵
      

  6.   


    php版本多少?我的是5.2.11,fopen返回 bool(true) 
      

  7.   

    if (file_exists("2.txt".$url)) {
    echo "存在相同文件名的文件";
    exit;
    你用他试试,我用的就能够判断是否有同文件名存在。