1

UPDATE: this problem was solved, my issue was a typo. The concept of what I was doing was correct.

I have a check if an image exits when I first display my page. It prints out:

if (@file_get_contents($url) != false)
{
     // show URL
}
else {
    // Show Placeholder Image
}

This code works, when I have my page first load and the php is written with the HTML.

BUT on other occasions, when I do a jQuery .post or .get call (so basically ajax) doing the file check above ALWAYS fails. I have printed out the URL and it points to correct file (I can do this by unsupprressing the errors). Now, the url is a different domain then the original file that is being called, so not sure if it is a cross domain issue (it is also a website that belongs to me so I have full access to the files).

Is this code not working due to ajax? Cross Domain? something else?

Also, I have tried file_exists, getimagesize and cUrl as well to no avail.

EDIT:

Here is my message when errors are not suppressed:

Warning: file_get_contents(http://www.---.---) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in --- on line 352

TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259

1 Answers1

1

If PHP is failing you in this case, try using jQuery to achieve the same result. There was another answer on SO with a little function that does just that: It checks if url is pointing to existing document or file, and returns its HTTP code.

https://stackoverflow.com/a/9377204/536425

Try it both with async: false and async: true, and see which one does the job for you.

Community
  • 1
  • 1
parrker9
  • 958
  • 14
  • 23
  • 1
    This won't work if the image is in a different domain from the web page, because of the AJAX same-domain requirement. – Barmar Sep 14 '15 at 01:01
  • @Barmar OP might be able to add the appropriate CORS headers seeing as they mention the remote site belongs to them – Phil Sep 14 '15 at 01:06
  • This might be difficult as I need to loop through a list and build my html server-side based on database results. If I was needing to check only for a single image I'd try this, – TheLettuceMaster Sep 14 '15 at 01:10
  • Lots of folks here in SO have advised that async false is not the way to go. Even Google Chrome browser will (in console) at times advise to keep calls async true (or leave at default which is true) – Adam T Sep 14 '15 at 01:20