file_exists with remote hosts?

I ran into this problem a few hours ago, and have yet to find a solution. Now its gotten to the point that its bothering me. Everything I try always returns true or false.

I’m trying to check to see if this image (www.site.com/image.png) exists. If it doesn’t exist, it will just open up a blank page.

So, If $opt1 exists, set $var = $opt1. If $opt1 doesn’t exist, set $var = $opt2. If it

I’ve tried it like;

if(file_exists($opt1))
		{
			$var = $opt1;
			} else {
			$var = $opt2;
		}

I was told file_exists may not work with remote hosts, so I tried something else;

if(is_null($opt1))
		{
			$var = $opt1;
			} else {
			$var = $opt2;
		}

That also didn’t work, so I tried;

if(fopen($opt1, "r"))
		{
			$var = $opt1;
			} else {
			$var = $opt2;
		}

None of the above worked, they are all returning the same value, even $opt1 exists. The problem may be that it’s not returning me a 404 error, but simply a page without the image.

Any other ideas? Am I doing something wrong? I’m still new to PHP and could be doing a very basic mistake, I’m just not sure. Please shed some light on me!

I’m pretty sure file_exists() doesn’t work for files on remote servers. But fopen() does.

This code works for me.

<?php

$handle = @fopen('http://www.google.com.au/images/nav_logo95.png','r');

if(!$handle) {
    echo 'file not found';
} else {
    echo 'file exists';
}

?>

Check the fopen() documentation for further info.

This still doesn’t work, maybe it will help if I provide an exact url with an example of what I’m talking about.

Test: http://avatar.xboxlive.com/avatar/test/avatarpic-l.png

Example: http://avatar.xboxlive.com/avatar/example/avatarpic-l.png

I don’t recieve a 404 error when looking at the “test” url, it just opens a blank page without anything on it. If you open the “example” url, it will open an image. Now I’m trying to sort through them and change all the ones with no image, to a default, preset image.

Maybe now that I gave real examples something will come out of it. Thanks for the help so far!

The code I posted works for me.

I also tried your test url and the code below echos “file exists” for me.

<?php

$handle = @fopen('http://avatar.xboxlive.com/avatar/test/avatarpic-l.png','r');

if(!$handle) {
    echo 'file not found';
} else {
    echo 'file exists';
}

?>

When I use your Example url as well, the above code also echos “file exists”.

That’s the problem. I need the “test” case to return false, but the “example” case to return true. They are both returning the same value.

check and see if allow_url_fopen is set to true in your php.ini.

I use something like this:


$headers= get_headers("http://avatar.xboxlive.com/avatar/example/avatarpic-l.png", 1);
if(strpos($headers[0], "404")!== false) {
  // file does not exist
} else {
  // file exists
}

More info here: http://nl2.php.net/get_headers

Which will bork as soon as you encounter a 3XX message. :wink:

(Receiving a 3XX doesnt tell you if the file it’s redirecting you to exists or not.)

Ah, good point. I guess then it’s time for a cURL solution?


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://avatar.xboxlive.com/avatar/test/avatarpic-l.png');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$res = curl_exec($ch);
$info = curl_getinfo($ch);
if($info['http_code']==404) {
    // file not found
}

Downside of this method is that it also downloads the actual image.

Curlopt_nobody ?

What exactly differentiates the result of the test URL’s response from any other success (or fail) response? It returns a 200 OK response which doesn’t say “I don’t exist!”

The image actually is there on the example url, whereas if you go to the test url, there is no image. The test url instead opens a blank page, not a 404 as it would have thought too.