user1904273
user1904273

Reputation: 4764

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.

Upvotes: 2

Views: 1078

Answers (1)

miken32
miken32

Reputation: 42712

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).

Upvotes: 2

Related Questions