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.
@. it's the coding equivalent of stuffing your fingers in your ears and going "lalalalala can't hear you". It also hides any message that PHP could be TRYING to give to you to tell you what the problem is. Plus, chaining calls like that is simply bad programming. If the fopen fails, it returns boolean false, which you then try to fclose, causing further errors. Never EVER assume success with external resources.$fh = fopen(...); if (!$fh) { return false; } fclose($fh); return true;