7

This spits out a whole heap of NO's but the images are there and path correct as they are displayed by <img>.

foreach ($imageNames as $imageName) 
{
    $image = 'http://path/' . $imageName . '.jpg';
    if (file_exists($image)) {
        echo  'YES';
    } else {
        echo 'NO';
    }
    echo '<img src="' . $image . '">';
}
TijuanaKez
  • 1,472
  • 3
  • 20
  • 28
  • 2
    The URL of an image file and the path to the same image file on the server's file system are **two different strings**. –  Feb 11 '13 at 05:04
  • Ok that makes sense. I'd been mislead by skimming over other posts using remote paths with file_exists. – TijuanaKez Feb 11 '13 at 06:10

5 Answers5

16

file_exists uses local path, not a URL.

A solution would be this:

$url=getimagesize(your_url);

if(!is_array($url))
{
     // The image doesn't exist
}
else
{
     // The image exists
}

See this for more information.

Also, looking for the response headers (using the get_headers function) would be a better alternative. Just check if the response is 404:

if(@get_headers($your_url)[0] == 'HTTP/1.1 404 Not Found')
{
     // The image doesn't exist
}
else
{
     // The image exists
}
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • 3
    This is very expensive. The entire image is downloaded before the information is retrieved. You are better off just looking at the headers of your request. Look into get_headers(). http://php.net/manual/en/function.get-headers.php – Gene Kelly Jun 13 '15 at 16:20
  • @GeneKelly Thanks! Updated. – Ionică Bizău Jun 13 '15 at 18:37
  • That's a superb answer (with the headers). But does it always return the same headers on different servers? Sorry for my ignorance on this. – itsols Sep 16 '16 at 11:10
  • Alternatively you can also use curl and check the http status code returned. if 404, distant image does not exist. – SuN Mar 02 '17 at 22:42
15

file_exists looks for a local path, not an "http://" URL

use:

$file = 'http://www.domain.com/somefile.jpg';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}
2
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($retcode==200) echo 'YES';
else              echo 'NO';
realization
  • 587
  • 1
  • 4
  • 5
1

This is what I did. It covers more possible outcomes with getting the headers because if you can't access the file it's not always just "404 Not Found". Sometimes it's "Moved Permanently", "Forbidden", and other possible messages. However it's just " 200 OK" if the file exists, and can be accessed. The part with HTTP can have 1.1, or 1.0 after it, which is why I just used strpos to be more reliable for every situation.

$file_headers = @get_headers( 'http://example.com/image.jpg' );

$is_the_file_accessable = true;

if( strpos( $file_headers[0], ' 200 OK' ) !== false ){
    $is_the_file_accessable = false;
}

if( $is_the_file_accessable ){
    // THE IMAGE CAN BE ACCESSED.
}
else
{
    // THE IMAGE CANNOT BE ACCESSED.
}
0
function remote_file_exists($file){

$url=getimagesize($file);

if(is_array($url))
{

 return true;

}
else {

 return false;

}

$file='http://www.site.com/pic.jpg';

echo remote_file_exists($file);  // return true if found and if not found it will return false
Fujael
  • 107
  • 1
  • 10