douju1852 2014-08-18 01:29
浏览 321
已采纳

PHP使用file_get_contents()检查外部服务器上是否存在文件

Checking file exists on external server using file_get_contents() method, does this method will work properly?

$url_file = "http://website.com/dir/filename.php";
$contents = file_get_contents($url_file);

if($contents){
echo "File Exists!";
} else {
echo "File Doesn't Exists!";
}
  • 写回答

3条回答 默认 最新

  • duanhuang4306 2014-08-18 04:00
    关注

    I think best method for me is using this script:

    $file = "http://website.com/dir/filename.php";
    $file_headers = get_headers($file);
    

    If file not exists output for $file_headers[0] is: HTTP/1.0 404 Not Found or HTTP/1.1 404 Not Found

    Use strpos method to check 404 string if file doesn't exists:

    if(strpos($file_headers[0], '404') !== false){
    echo "File Doesn't Exists!";
    } else {
    echo "File Exists!";
    }
    

    Thanks for all help :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料