Jump to content

Recommended Posts

What is the best/proper way to run an image URL from an external site through a check to see if the image actually exists?  I am trying to use file_exists() but it's not recognizing image URLs from external sites as existing, so there must be some other way to deal with external URLs.  In other words, the image does exist but file_exists() says it doesn't.

 

Can anyone help?

 

 

Link to comment
https://forums.phpfreaks.com/topic/254851-check-if-external-url-image-exists/
Share on other sites

I suppose that you could use file_get_contents() (if fopen wrappers are enabled).  this will return false if the file can't be accessed.  I don't think the external site would like you hitting the image twice though, they may not even like you hitting the image at all.

Ok, I tried file_get_contents() and it's properly recognizing that an image is OK, but if the image doesn't exist, it still gives this warning...

 

Warning:  file_get_contents(http://mysite.s3.amazonaws.com/1/monkey.jpg) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

 

I really just need a way to essentially say "Hey PHP, if you're going to kick a warning about this specific image URL, run this code instead and DON'T show the warning" :)  Obviously it's not that easy because Googling about this finds lots of different suggestions, and a lot of people saying the suggestions don't do what they need.

 

FYI, my new code is...

 

if (file_get_contents($image)) {  //image URL is OK
											echo "<span class='image'><img src='".$image."'  class='image' title = '".$image."' ";
												list($width,$height) = getimagesize($image);
												if ($width > $height) {
													echo "width=50 /></span>";
												} elseif ($height > $width) {
													echo "height=50 /></span>";
												} else { //height and width must be equal so just set width = 50, but could just as easily set height to 50
													echo "width=50 /></span>";
												}

										} else {  //image URL must not be OK
											echo "<span class='image'><img src='/images/noimage.png'  class='image' title = 'no image'  /></span>";

										}

 

So if the image doesn't work it does echo that last span (which I want I to do), but it still shows the warning. So I need to get rid of that warning.  And I don't want to just turn off warnings :), I want to properly change the code so the warning doesn't happen regardless.

 

If anyone has any other suggestions, I am more than open to them  :confused:

 

Thanks!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.