Correct PHP way to check if external image exists?

后端 未结 9 1323
清歌不尽
清歌不尽 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: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

提交回复
热议问题