file_exists() returns false even if file exist (remote URL)

前端 未结 6 1893
走了就别回头了
走了就别回头了 2020-12-03 07:54

My file_exists() returns false even if provided image to check https://www.google.pl/logos/2012/haring-12-hp.png exist. Why?

Below I am pre

相关标签:
6条回答
  • 2020-12-03 08:19

    What you need is something like url_exists. Refer to the comments in the file_exists docs: http://php.net/manual/en/function.file-exists.php

    Here's one of the examples posted:

    <?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;}
        }
    ?>
    
    0 讨论(0)
  • 2020-12-03 08:21
    $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";
    }
    
    0 讨论(0)
  • 2020-12-03 08:25
    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;
    
    }
    
    0 讨论(0)
  • 2020-12-03 08:26

    A better if statement that not looks at http version

    $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
    }
    
    0 讨论(0)
  • 2020-12-03 08:32

    As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.

    From the http(s) page on Supported Protocols and Wrappers:

    Supports stat()   No
    
    0 讨论(0)
  • 2020-12-03 08:38
        $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";
    
        }
    
    0 讨论(0)
提交回复
热议问题