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.

8
  • 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. Commented Oct 27, 2015 at 14:57
  • How would I disentangle it? Wrote this code several years ago and frankly am rusty on php. Commented Oct 27, 2015 at 15:03
  • 2
    You'd have to rewrite it. Try googling it, instead. stackoverflow.com/questions/1363925/… stackoverflow.com/questions/14806159/… stackoverflow.com/questions/10489210/… stackoverflow.com/questions/981954/… As you can see, this is a duplicate subject here really. This has been answered on SO many times. Commented Oct 27, 2015 at 15:05
  • 3
    $fh = fopen(...); if (!$fh) { return false; } fclose($fh); return true; Commented Oct 27, 2015 at 15:05
  • 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? Commented Oct 27, 2015 at 15:23

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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. stackoverflow.com/questions/7991425/… 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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.