gambrker Posted July 14, 2011 Share Posted July 14, 2011 My goal is simple. I want to check a URL for an image. If the image is there, do this. If not, do this. In its simplest form, I cannot get it to work. Any help is appreciated. Running VPS, PHP 5.1.6. The image is there, but its still saying image does not exist. http://www.communitychurch.cc/if-else-test2.php <?php $filename = 'http://www.communitychurch.cc/site-uploads/about/header-image.jpg'; if (file_exists($filename)) { echo "The file $filename exists"; } else { echo "The file $filename does not exist"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241983-file_exists-function-not-working/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2011 Share Posted July 14, 2011 Your $filename isn't a filename, it's a URL. You cannot use a http URL in the file_exists function. You must use a file system path as an argument to the file_exists function. Is this image located on your server or some other server? Quote Link to comment https://forums.phpfreaks.com/topic/241983-file_exists-function-not-working/#findComment-1242684 Share on other sites More sharing options...
premiso Posted July 14, 2011 Share Posted July 14, 2011 Your $filename isn't a filename, it's a URL. You cannot use a http URL in the file_exists function. You must use a file system path as an argument to the file_exists function. I thought this was only true if the allow_url_fopen were turned off. Just did a test, looks like you are right Either or, I would do it with cURL Check here for a function that should do what you want: http://php.net/manual/en/function.file-exists.php#85246 Quote Link to comment https://forums.phpfreaks.com/topic/241983-file_exists-function-not-working/#findComment-1242685 Share on other sites More sharing options...
AyKay47 Posted July 14, 2011 Share Posted July 14, 2011 As PFM said, this is not a file _path, its a URL. Also, file_exists will return not work when used on remote severs Quote Link to comment https://forums.phpfreaks.com/topic/241983-file_exists-function-not-working/#findComment-1242687 Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2011 Share Posted July 14, 2011 file_exists uses stat to get the file information. http/https doesn't support stat. Quote Link to comment https://forums.phpfreaks.com/topic/241983-file_exists-function-not-working/#findComment-1242690 Share on other sites More sharing options...
gambrker Posted July 14, 2011 Author Share Posted July 14, 2011 The image is located on my server, but I must use a URL because the URL will actually be generated by a system for more than 1 image. I am not curl savvy (and obviously a novice PHP coder), what function should I use besides file_exists? Quote Link to comment https://forums.phpfreaks.com/topic/241983-file_exists-function-not-working/#findComment-1242694 Share on other sites More sharing options...
premiso Posted July 14, 2011 Share Posted July 14, 2011 I am not curl savvy (and obviously a novice PHP coder), what function should I use besides file_exists? Did you take a look at the link I posted? It is just a function that you can copy and paste and it will work. Quote Link to comment https://forums.phpfreaks.com/topic/241983-file_exists-function-not-working/#findComment-1242701 Share on other sites More sharing options...
gambrker Posted July 14, 2011 Author Share Posted July 14, 2011 Honestly, I got a little intimidated and wasn't sure what to do with it. I gave it a try and it works like a charm. Thank you all! Final Code: <?php function url_exists($url) { // Version 4.x supported $handle = curl_init($url); if (false === $handle) { return false; } curl_setopt($handle, CURLOPT_HEADER, false); curl_setopt($handle, CURLOPT_FAILONERROR, true); // this works curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox curl_setopt($handle, CURLOPT_NOBODY, true); curl_setopt($handle, CURLOPT_RETURNTRANSFER, false); $connectable = curl_exec($handle); curl_close($handle); return $connectable; } $imageURL = 'http://www.communitychurch.cc/site-uploads/about/header-image.jpg'; if (url_exists($imageURL)) { echo "The file $imageURL exists"; } else { echo "The file $imageURL does not exist"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241983-file_exists-function-not-working/#findComment-1242706 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.