Wednesday, November 13, 2013

How to check whether a remote file exists in PHP

Sometimes, we may need to execute remote php files and work on the resultant output. For example, very recently I have worked on a project in which file-checking PHP scripts on various remote servers are executed at regular intervals by a cron set on a single server at my disposal [I can control this server]. Those file-checking PHP scripts check if any file has been modified recently on its local server. This facility helps in tracking if any file has been modified by recent hacking attack.

In this situation, I needed to use CURL methods to execute the remote scripts, then get and parse the output. But I got error messages when the remote file-checking scripts were removed/deleted/renamed by hacking or any other ways. So, before executing remote scripts, we needed to check if the script existed at all.

There are many ways we can determine whether a remote script exists or not.

1. The first method is using functions like fopen()/file_get_contents()/file_exists() method. Check the code below..

<?php

/// We are trying to open the remote file in READ-ONLY mode
/// Using fopen() function 
$fp = fopen("http://example.org/file_check.php","r") or die("Could not open remote file");

/// Using file_get_contents() function
$str = file_get_contents("http://example.org/file_check.php") or die("Could not open remote file");

/// Using file_exists() function
echo  file_exists("http://example.org/file_check.php") or die("ERROR");

?>

2. The second method is using the function get_headers(). This function returns all the header sent by the server. Check the code below.

<?php

// GET the URL status
$status = get_headers( 'http://example.org/file_check.php' , 1) ;

// Check the status CODE
if( $status[0] == 'HTTP/1.0 404 Not Found' )
{
  // ... 
}

?>

The get_headers() function returns the headers in an array format and that array's zeroth position always hold the response code. 

All the above methods would require the allow_url_fopen settings in php.ini file to be set to 'On'. Otherwise they would fail.

3. The third method is to use CURL library. Check the code below.

<?php

// Init CURL
$ch = curl_init();

// set URL and other appropriate options
$curl_url = "http://example.org/file_check.php";
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );

// grab URL and pass it to the browser
$str = curl_exec($ch);

/// Check if the file_check.php exists
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

/// HTTP Status 200 means file exists
if( $http_status == "200" )
{
   //// The result/Output is already stored in $str
   //// We will parse the value of $str
}

?>

The above program is quite self-explanatory.  The curl_getinfo() function returns the response code which is 200 in case the page exists. The CURL method does not care about the value set to allow_url_fopen directive, hence very convenient and powerful tool to use.

No comments: