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 . '">';
}
2
  • 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. Commented Feb 11, 2013 at 5:04
  • Ok that makes sense. I'd been mislead by skimming over other posts using remote paths with file_exists. Commented Feb 11, 2013 at 6:10

5 Answers 5

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

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(). php.net/manual/en/function.get-headers.php
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.
Alternatively you can also use curl and check the http status code returned. if 404, distant image does not exist.
Option with get_headers() shows, that image exists, even if link doesn't contain an image. Try if(@get_headers('https://stackoverflow.com/questions/14806159/how-do-i-test-if-an-remote-image-file-exists-in-php')[0] == 'HTTP/1.1 404 Not Found') { ...
That's why we also should search for a value, that contains Content-Type: image/.
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;
}

5 Comments

@Phil have you tried it with a URL? It returns false: codepad.viper-7.com/56dDzt
if the server returns: HTTP/1.1 403 ?
If it's 403, the image is still not accessible, but exists.
Yes this works but John's answer is simpler and won't fail with 403 or typos in the header.
Good solution. To check if the file is accessible, you could do the following: $file_headers[0] != 'HTTP/1.1 200 OK'
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';

Comments

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.
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.