php 判断远程文件是否存在

bupt_lash 2009-11-05 10:36:04
<?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
...全文
479 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
YIDAY 2009-11-06
  • 打赏
  • 举报
回复
is_readable()
do_fork 2009-11-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 bupt_lash 的回复:]
引用 7 楼 do_fork 的回复:
引用 6 楼 bupt_lash 的回复:
引用 4 楼 do_fork 的回复:
引用 3 楼 bupt_lash 的回复:
file_exists不能用于远程文件吧?and另外的错误来了,
Fatal error: Call to undefined function   fwrite() in D:\Apache Group\Apache2\htdocs\play\exist.php on line 52

太诡异了


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


这个还是没搞定,不过我是用来进行下载的,是不是提供下载链接就ok啦,不用判断了呢?


服务器端下载,还是提供给客户端下载?


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

php版本多少?

我的是5.2.11,fopen返回 bool(true)
bupt_lash 2009-11-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 do_fork 的回复:]
引用 6 楼 bupt_lash 的回复:
引用 4 楼 do_fork 的回复:
引用 3 楼 bupt_lash 的回复:
file_exists不能用于远程文件吧?and另外的错误来了,
Fatal error: Call to undefined function   fwrite() in D:\Apache Group\Apache2\htdocs\play\exist.php on line 52

太诡异了


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


这个还是没搞定,不过我是用来进行下载的,是不是提供下载链接就ok啦,不用判断了呢?


服务器端下载,还是提供给客户端下载?
[/Quote]

提供给客户端下载,非常感谢回答啦!fopen还没搞定的说,呵呵
do_fork 2009-11-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 bupt_lash 的回复:]
引用 4 楼 do_fork 的回复:
引用 3 楼 bupt_lash 的回复:
file_exists不能用于远程文件吧?and另外的错误来了,
Fatal error: Call to undefined function   fwrite() in D:\Apache Group\Apache2\htdocs\play\exist.php on line 52

太诡异了


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


这个还是没搞定,不过我是用来进行下载的,是不是提供下载链接就ok啦,不用判断了呢?
[/Quote]

服务器端下载,还是提供给客户端下载?
bupt_lash 2009-11-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 do_fork 的回复:]
引用 3 楼 bupt_lash 的回复:
file_exists不能用于远程文件吧?and另外的错误来了,
Fatal error: Call to undefined function   fwrite() in D:\Apache Group\Apache2\htdocs\play\exist.php on line 52

太诡异了


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

这个还是没搞定,不过我是用来进行下载的,是不是提供下载链接就ok啦,不用判断了呢?
do_fork 2009-11-05
  • 打赏
  • 举报
回复
file_exists 并不是完全不能用于远程文件,而是要求这个远程文件的wrapper必须支持 stat 系列函数

由于http协议不支持这个函数,所以不行,ftp是可以的

<?php
clearstatcache();
var_dump(file_exists("ftp://ftp.debian.org/debian/"));
?>
do_fork 2009-11-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bupt_lash 的回复:]
file_exists不能用于远程文件吧?and另外的错误来了,
Fatal error: Call to undefined function   fwrite() in D:\Apache Group\Apache2\htdocs\play\exist.php on line 52

太诡异了
[/Quote]

fopen肯定可以,试试看

var_dump(fopen("http://www.baidu.com/img/baidu_logo.gif", "r"));
bupt_lash 2009-11-05
  • 打赏
  • 举报
回复
file_exists不能用于远程文件吧?and另外的错误来了,
Fatal error: Call to undefined function   fwrite() in D:\Apache Group\Apache2\htdocs\play\exist.php on line 52

太诡异了
mingfish 2009-11-05
  • 打赏
  • 举报
回复
果然学习了
do_fork 2009-11-05
  • 打赏
  • 举报
回复
php5自带的 file_exists 支持url,
直接用就行了
debiangrub 2009-11-05
  • 打赏
  • 举报
回复
角马不是马很牛啊。
lyp71887188 2009-11-05
  • 打赏
  • 举报
回复
if (file_exists("2.txt".$url)) {
echo "存在相同文件名的文件";
exit;
你用他试试,我用的就能够判断是否有同文件名存在。

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧