How to know if a file exists on the web or not, in PHP?

I have listed a few methods in PHP to check the existence of a file on the web.

<?php
$fileURL = "http://www.some-domain.com/image.jpg";
echo "Checking the existence of $fileURL<br>";
?>

Method 1:

<?php
$a = microtime(true);                                    //start time in microseconds of 1st method
echo date("d/m/y : H:i:s", time()), " ",$a, "<br>";
get_http_response_code($fileURL);      //fast but not fool proof
echo "<br>";
$b = microtime(true);                                   //start time in microseconds of 2nd method
echo date("d/m/y : H:i:s", time()), " ", $b, "<br>";
echo "difference = ",$b-$a,"<br>";           //difference between 2nd and 1st
?>

Method 2:

<?php
isPartOfString($fileURL);
echo "<br>";
$c = microtime(true);                                  //start time in microseconds of 3rd method
echo date("d/m/y : H:i:s", time()), " ", $c, "<br>";
echo "difference = ",$c-$b,"<br>";         //difference between 3rd and 2nd
?>

Method 3:

<?php
matchCode($fileURL);
echo "<br>";
$d = microtime(true);
echo date("d/m/y : H:i:s", time()), " ", $d, "<br>";
echo "difference = ",$d-$c,"<br>";
?>

Method 4:

<?php
check_url($fileURL);
echo "<br>";
$e = microtime(true);
echo date("d/m/y : H:i:s", time()), " ", $e, "<br>";
echo "difference = ",$e-$d,"<br>";
?>

Method 5:

<?php
http_file_exists($fileURL);
$f = microtime(true);
echo "difference = ",$f-$e,"
";
?>
<?php
function isPartOfString($fileURL)
{
   $getHeaders = @get_headers($fileURL);
   $str = $getHeaders[0];
   // 200 is the response code in the very first line of the headers when some file is found.
   // 3rd param starts search after HTTP/1
   $pos = strpos($str, ' 200 ', 5);   // Note the space before and after 200

   // Note: The !== operator should be used below.
   // Using != would not work as expected some times, e.g,
   // if the position of second param in the $str is 0 then statement (0 != false) evaluates
  //to false, though it was found....so take care of context also while forming conditions

   if ($pos !== false) {
   // now we can be sure that the func strpos returned something not in boolean context
      //returned pos is irrestive of offset
      if($pos > 5)
      {
         echo "File found";
      }
   }
   else {
      //echo "The string '$findme' was not found in the string '$mystring'";
      echo "File not found";
   }
}
?>
<?php
function matchCode($theURL)
{
   $headers = get_headers($theURL);
   //this function uses regular expression engine, so might be a bit slow
   if (preg_match("/\s200\s/", $headers[0])) {
      echo "File found";
   }
   else  {
      echo "File not found";
     }
}
?>
<?php
function get_http_response_code($theURL) {
   $headers = get_headers($theURL);

   //this works only when position of the response code (200) dosn't alter in future
   if (substr($headers[0], 9, 3) == 200) 
   echo "File found";
   else
   echo "File not found";
}
?>
<?php
function check_url($url) {
   $c = curl_init();
   curl_setopt($c, CURLOPT_URL, $url);
   curl_setopt($c, CURLOPT_HEADER, 1); // get the header
   curl_setopt($c, CURLOPT_NOBODY, 1); // body not required
   curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); // get response as a string from curl_exec, not echo it
   curl_setopt($c, CURLOPT_FRESH_CONNECT, 1); // don't use a cached version of the url
   if (!curl_exec($c)) {
      return false;
   }

   $httpcode = curl_getinfo($c, CURLINFO_HTTP_CODE);
   if($httpcode === 200)   // not <400
   {
       echo "File found $httpcode <br>";
   }
   else
   {
        echo "File not found<br>";
   }
   #return ($httpcode < 400);
}
?>
<?php
function http_file_exists($url)
{
   $f = @fopen($url,"r"); //supress warnings by @
   if($f)
   {
      fclose($f);
      echo "File found ";
      return true;
   }
   echo "File not found ";
   return false;
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>