2

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.

altocumulus
  • 21,179
  • 13
  • 61
  • 84
user1904273
  • 4,562
  • 11
  • 45
  • 96
  • 6
    stop using `@`. 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. – Marc B Oct 27 '15 at 14:57
  • How would I disentangle it? Wrote this code several years ago and frankly am rusty on php. – user1904273 Oct 27 '15 at 15:03
  • 2
    You'd have to rewrite it. Try googling it, instead. http://stackoverflow.com/questions/1363925/check-whether-image-exists-on-remote-url http://stackoverflow.com/questions/14806159/how-do-i-test-if-an-remote-image-file-exists-in-php http://stackoverflow.com/questions/10489210/check-if-remote-images-exist-php http://stackoverflow.com/questions/981954/how-can-one-check-to-see-if-a-remote-file-exists-using-php As you can see, this is a duplicate subject here really. This has been answered on SO many times. – Mikel Bitson Oct 27 '15 at 15:05
  • 3
    `$fh = fopen(...); if (!$fh) { return false; } fclose($fh); return true;` – Marc B Oct 27 '15 at 15:05
  • @mikel, yes I have other methods that check using curl and also getimagesize but I would like to debug this one. Also wondering if PHP version might affect anything. – user1904273 Oct 27 '15 at 15:20
  • 1
    Hey @user1904273, that's a good mindset. Figuring it out will be good in the long run. The problem is, depending on the server configuration, you may or may not be able to actually utilize fopen on a URL. This can be changed in php.ini using the allow_url_fopen parameter. This means that the code will be less portable if you utilize fopen over curl- as the curl method will work regardless of this php.ini setting. If you're rewriting it now, why rewrite it in a way that is already dated? – Mikel Bitson Oct 27 '15 at 15:23
  • I have a feeling my host may have changed something that has broken the code. I already have the curl and get image size methods in the code. I removed the fopen check, however, and while the files are now uploading they are solid black so there is still something wrong. – user1904273 Oct 27 '15 at 15:38
  • Your statement would return `true` or `false` depending on whether `fclose()` executed successfully or not. – 131 Oct 27 '15 at 20:44

1 Answers1

2

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

miken32
  • 42,008
  • 16
  • 111
  • 154
  • It would be awesome if this worked, however, when I tried it, it retuned null. There seem to be mixed results with this and it requires allow_url_fopen to be enabled. http://stackoverflow.com/questions/7991425/php-how-to-check-if-image-file-exists I suspect my host has closed url_open as I can no longer use it or this suggestion. I am trying to chat with host but perennially busy. – user1904273 Oct 27 '15 at 23:41
  • That is correct. Also, testing in my environment with your fopen/fclose method works so you're probably looking in the right direction with hosting restrictions. – miken32 Oct 27 '15 at 23:48