Home  »     »     »     »  How to check remote file or image exists

How to check remote file or image exists

Wednesday, May 16, 2012

Php scripts to check remote file exists.

Check files in same server

To check a file in the same server use file_exists(), this doesn't support remote files
file_exists — Checks whether a file or directory exists
example :

<?php
if( file_exists("lib/class.php") )
echo "file present";
else
echo "file not found ";
?>


The limitation of file_exists() is , it doesn't support remote files. To check for remote file you can use any of the following:

#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|


Check Remote Image

If you are checking for an image, You can use function getimagesize()

Unlike file_exists, this built-in function supports remote files. It will return an array that contains the image information (width, height, type..etc). So check the returned array to confirm the presense of image

<?php
$arrImg = getimagesize("http://www.remoteserver.com/img.jpg");
if($arrImg[0])
{
echo "Image information
";
print_r($arrImg);
}
else
{
echo "invalid image / Image not present";
}
?>

#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|

Check header of remote files

You can check the header info of a remote file using get_headers() function. get_headers — Fetches all the headers sent by the server in response to a HTTP request. The header info can be processed to find the status of the file.

usage of get_headers():

<?php

$url = 'http://www.remoteserver.com';

print_r(get_headers($url));

print_r(get_headers($url, 1));

?>

How to checked the status code in the header inforamtion returned by get_headers:

<?php

function remote_file_exists($remote_url){
return(bool)preg_match('~HTTP/1\.\d\s+200\s+OK~', @current(get_headers($url)));
}

$url = "http://www.remoteserver.com/data.php";
if( remote_file_exists($url) ){
echo "file found!";
}
else{
echo "file not found!!!";
}



?>

#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|

Checking the HTTP Status code with file_get_contents() and stream_context_set_default()

obtaining the HTTP Status code (404 = not found) which is possible with file_get_contents making use of context options.

<?php

$url = 'http://remoteserver.com/';
$code = FALSE;

$options['http'] = array(
'method' => "HEAD",
'ignore_errors' => 1,
'max_redirects' => 0
);

$body = file_get_contents($url, NULL, stream_context_create($options));

sscanf($http_response_header[0], 'HTTP/%*d.%*d %d', $code);

echo "Status code: $code";


?>

#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|#|

Check files using Curl

You can get cURL to only give you the headers, and not the body, which might make it faster. If you are giving an invalid domain, then it may take some time, so You can set the curl request timeout param.

<?php
function remoteFileExists($url) {
$curl = curl_init($url);

//don't fetch the actual page, you only want to check the connection is ok
curl_setopt($curl, CURLOPT_NOBODY, true);

//do request
$result = curl_exec($curl);

$ret = false;

//if request did not fail
if ($result !== false) {
//if request was ok, check response code
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($statusCode == 200) {
$ret = true;
}
}

curl_close($curl);

return $ret;
}

$exists = remoteFileExists('http://remoteserver/img.jpg');
if ($exists) {
echo 'file exists';
} else {
echo 'file does not exist';
}

?>



How to link to this page?
If you wish to link to this page from your website, simply Copy and paste the above HTML code to your web page. It will appear on your page as:
How to check remote file or image exists.




Share this!




comments powered by Disqus

This Weeks 7 Popular Posts


Subscribe to Recent Posts by Email
Stay connected to CROZOOM with regular Email notices of new Techie articles and IT Jobs. Updates will be delivered to your Inbox as soon as they are posted online.

Enter Your Email Address:  

Delivered by FeedBurner   RSS Feed

Search this Blog   



Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »