galvin Posted January 12, 2012 Share Posted January 12, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/254851-check-if-external-url-image-exists/ Share on other sites More sharing options...
jcbones Posted January 12, 2012 Share Posted January 12, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254851-check-if-external-url-image-exists/#findComment-1306766 Share on other sites More sharing options...
galvin Posted January 13, 2012 Author Share Posted January 13, 2012 thanks jcbones, I'll give it a shot. There may be a few external site URLs that are sites not owned by me, but I am likely using Amazon S3 for the majority of my image storage Quote Link to comment https://forums.phpfreaks.com/topic/254851-check-if-external-url-image-exists/#findComment-1307102 Share on other sites More sharing options...
galvin Posted January 13, 2012 Author Share Posted January 13, 2012 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 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/254851-check-if-external-url-image-exists/#findComment-1307109 Share on other sites More sharing options...
laffin Posted January 13, 2012 Share Posted January 13, 2012 shure it's easy if(@file_get_contents($image)) { } else { } Quote Link to comment https://forums.phpfreaks.com/topic/254851-check-if-external-url-image-exists/#findComment-1307112 Share on other sites More sharing options...
galvin Posted January 13, 2012 Author Share Posted January 13, 2012 I'll be damned, that worked. I guess this "solution" would be seen as hacky, but it will do for now Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/254851-check-if-external-url-image-exists/#findComment-1307114 Share on other sites More sharing options...
.josh Posted January 13, 2012 Share Posted January 13, 2012 you can use cURL to get the image or a response. Quote Link to comment https://forums.phpfreaks.com/topic/254851-check-if-external-url-image-exists/#findComment-1307119 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.