Correct PHP way to check if external image exists?

后端 未结 9 1298
清歌不尽
清歌不尽 2021-02-04 08:49

I know that there are at least 10 the same questions with answers but none of them seems to work for me flawlessly. I\'m trying to check if internal or external image exists (is

相关标签:
9条回答
  • 2021-02-04 09:30
    function checkExternalFile($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_exec($ch);
        $retCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
    
        return $retCode;
    }
    
    $fileExists = checkExternalFile("http://example.com/your/url/here.jpg");
    
    // $fileExists > 400 = not found
    // $fileExists = 200 = found.
    
    0 讨论(0)
  • 2021-02-04 09:36

    I know you wrote "without curl" but still, somebody may find this helpfull:

    function curl_head($url) {
    
        $ch = curl_init($url);
    
        //curl_setopt($ch, CURLOPT_USERAGENT, 'Your user agent');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1); # get headers
        curl_setopt($ch, CURLOPT_NOBODY, 1); # omit body
        //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); # do SSL check
        //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); # verify domain within cert
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); # follow "Location" redirs
        //curl_setopt($ch, CURLOPT_TIMEOUT_MS, 700); # dies after 700ms
    
        $result = curl_exec($ch);
    
        curl_close($ch);
    
        return $result;
    }
    
    print_r(curl_head('https://www.example.com/image.jpg'));
    

    You will see someting like this HTTP/1.1 200 OK or HTTP/1.1 404 Not Found in returned header array. You can do also multiple parallel requests with curl multi.

    0 讨论(0)
  • 2021-02-04 09:37

    There are multiple steps, there is no single solution:

    1. Validate URL
    2. Check whether the file is available (can be done directly with step 3)
    3. Download the image into a tmp file.
    4. Use getimagesize to check the size of the image.

    For this kind of work you can catch the exceptions and handle them well to define your answer. In this case you could even suppress errors because it's intended that they trick might fail. So you handle the errors correctly.

    Because it's not possible to do a 100% check on it without having the actual image downloaded. So step 1 and 2 are required, 3 and 4 optional for a more definitive answer.

    0 讨论(0)
  • 2021-02-04 09:47

    This code is actually to check file... But, it does works for images!

    $url = "http://www.myfico.com/Images/sample_overlay.gif";
    $header_response = get_headers($url, 1);
    if ( strpos( $header_response[0], "404" ) !== false )
    {
       // FILE DOES NOT EXIST
    } 
    else 
    {
       // FILE EXISTS!!
    }
    
    0 讨论(0)
  • 2021-02-04 09:48

    getimagesize($img) fails when image doesn't exist: am not sure you understand what you want .....

    FROM PHP DOC

    The getimagesize() function will determine the size of any given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondant HTTP content type.

    On failure, FALSE is returned.

    Example

    $img = array("http://i.stack.imgur.com/52Ha1.png","http://example.com/img.jpg");
    foreach ( $img as $v ) {
        echo $v, getimagesize($v) ? " = OK  \n" : " = Not valid \n";
    }
    

    Output

    http://i.stack.imgur.com/52Ha1.png = OK  
    http://example.com/img.jpg = Not valid 
    

    getimagesize works just fine

    • PHP 5.3.19
    • PHP 5.4.9

    Edit

    @Paul .but your question is essentially saying "How do I handle this so I won't get an error when there's an error condition". And the answer to that is "you can't". Because all these functions will trigger an error when there is an error condition. So (if you don't want the error) you suppress it. None of this should matter in production because you shouldn't be displaying errors anyway ;-) – DaveRandom

    0 讨论(0)
  • 2021-02-04 09:49

    If you're using PHP >=5.0.0 you can pass an additional parameter into fopen to specify context options for HTTP, among them whether to ignore failure status codes.

    $contextOptions = array( 'http' => array('ignore_errors' => true));
    
    $context = stream_context_create($contextOptions);
    
    $handle = fopen($url, 'r', false, $context);
    
    0 讨论(0)
提交回复
热议问题