Remote File Exists in PHP?

May 28th, 2012

There are quite a few different ways to test the existance of a file on a remote server using a URL.
Unfortunately, the file_exists() function doesn’t support remote URL’s so you can’t use that.

file_get_contents

This is the easiest way to test the existance of a remote file:

<?

$myfile = file_get_contents('http://www.mysite/myimage.gif');
if ($myfile !== false) {
   echo "yes";
} else {
   echo "no";
}

?>

fopen

<?

$myfile = "http://www.mysite/myimage.gif";
if(fopen($file,"r") == true) {
   echo "yes";
} else {
   echo "no";
}

?>