【问题标题】:file_exists() returns false even if file exist (remote URL)即使文件存在(远程 URL),file_exists() 也会返回 false
【发布时间】:2012-05-13 17:11:49
【问题描述】:

我的file_exists() 返回false,即使提供的图像检查https://www.google.pl/logos/2012/haring-12-hp.png 是否存在。为什么?

下面我将展示准备在 localhost 上触发的完整失败的 PHP 代码:

$filename = 'https://www.google.pl/logos/2012/haring-12-hp.png';
echo "<img src=" . $filename . " />";
if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

【问题讨论】:

  • 那么,你拥有谷歌,是吗? Checks whether a file or directory exists. 仅限本地。

标签: php


【解决方案1】:
$filename= 'https://www.google.pl/logos/2012/haring-12-hp.png';
$file_headers = @get_headers($filename);

if($file_headers[0] == 'HTTP/1.0 404 Not Found'){
      echo "The file $filename does not exist";
} else if ($file_headers[0] == 'HTTP/1.0 302 Found' && $file_headers[7] == 'HTTP/1.0 404 Not Found'){
    echo "The file $filename does not exist, and I got redirected to a custom 404 page..";
} else {
    echo "The file $filename exists";
}

【讨论】:

  • 如果链接而不是图像(不存在)转到 404 自定义错误页面怎么办?
  • 嘿,请看代码的新变化。现在检查它是否登陆了自定义 404 页面。
  • k 我不得不将标题从 if($file_headers[0] == 'HTTP/1.0 404 Not Found'){ 更改为 if($file_headers[0] == 'HTTP/1.1 404 Not Found'){ 谢谢,你真的帮助了我。
  • 好吧...我对 if 条件做了一些调整... if(strpos($file_headers[0], '200')>1 or strpos($file_headers[0], '304')>1) { echo "$filename 文件存在"; } 可能不是完美的解决方案,但是... :))
  • 我不得不将您的 if 语句更改为 if(strpos($file_headers[0], '404')){ //not exists }else{ //exist} code`,因为 Szymon Toda 提到了同一点。
【解决方案2】:

更好的 if 语句,不查看 http 版本

$file_headers = @get_headers($remote_filename);    
if (stripos($file_headers[0],"404 Not Found") >0  || (stripos($file_headers[0], "302 Found") > 0 && stripos($file_headers[7],"404 Not Found") > 0)) {
//throw my exception or do something
}

【讨论】:

    【解决方案3】:

    从 PHP 5.0.0 开始,这个函数也可以与一些 URL 包装器一起使用。请参阅支持的协议和包装器以确定哪些包装器支持 stat() 系列功能。

    来自http(s) pageSupported Protocols and Wrappers

    Supports stat()   No
    

    【讨论】:

    • 网址:http://services.runescape.com/m=itemdb_rs/3716_obj_sprite.gif?id=2
    • URL 是什么无关紧要; httphttps 不支持 stat()
    【解决方案4】:
    function check_file ($file){
    
        if ( !preg_match('/\/\//', $file) ) {
            if ( file_exists($file) ){
                return true;
            }
        }
    
        else {
    
            $ch = curl_init($file);
            curl_setopt($ch, CURLOPT_NOBODY, true);
            curl_exec($ch);
            $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
            if($code == 200){
                $status = true;
            }else{
                $status = false;
            }
            curl_close($ch);
            return $status;
    
        }
    
        return false;
    
    }
    

    【讨论】:

      【解决方案5】:
          $filename = "http://im.rediff.com/money/2011/jan/19sld3.jpg";
      
          $file_headers = @get_headers($filename);
      
          if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
          //return false; 
          echo "file not found";
          }else {
          //return true;  
          echo "file found";
      
          }
      

      【讨论】:

        【解决方案6】:

        你需要像url_exists 这样的东西。参考file_exists文档中的cmets:http://php.net/manual/en/function.file-exists.php

        这是发布的示例之一:

        <?php
            function url_exists($url){
                $url = str_replace("http://", "", $url);
                if (strstr($url, "/")) {
                    $url = explode("/", $url, 2);
                    $url[1] = "/".$url[1];
                } else {
                    $url = array($url, "/");
                }
        
                $fh = fsockopen($url[0], 80);
                if ($fh) {
                    fputs($fh,"GET ".$url[1]." HTTP/1.1\nHost:".$url[0]."\n\n");
                    if (fread($fh, 22) == "HTTP/1.1 404 Not Found") { return FALSE; }
                    else { return TRUE;    }
        
                } else { return FALSE;}
            }
        ?>
        

        【讨论】:

        • 那个例子很糟糕。最好只使用 cURL 发出 HEAD 请求。
        猜你喜欢
        • 2012-03-01
        • 2016-05-28
        • 2017-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-19
        相关资源
        最近更新 更多