简体   繁体   中英

PHP: check if image file exists using fopen

This question is about using fopen to check if a file exists, not cURL or getimagesize which are alternative methods but not what I am asking about.

I having been using following function in code for a couple years without problems and it is suddenly always returning FALSE , even on valid images. I don't know if I accidentally created a typo or if my host changed the version of PHP or what may have caused it but would appreciate it if anyone can spot what might be going wrong.

Here is code:

function image_exist($url) {
            if (@fclose(@fopen( $url,  "r "))) {
             // true;
return TRUE;
            } else {
             // false;
return FALSE;
            }
    }

This is now returning FALSE even on valid images.

Why use fopen() and fclose() when there's a function for this purpose?

function image_exist($url) {
    return file_exists($url);
}

Edit: you're correct that this does not work for remote files over HTTP(S).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM