Skip to main content

How to check remote file or image exists

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';
}

?>

Comments

Popular posts from this blog

How to apply for a new ration card and what are the documents required?

List of documents required for new ration card application in Kerala and how to apply


Application for a new ration card should be addressed to Taluk Supply Officer (TSO) / City Rationing Officer (CRO) of applicant's residing area.

Primary document required are Residence certificate, Income certificate and incase the applicant's name is in another ration card then he/she should submit the reduction certificate ontained from previous TSO / CRO as proof for removing his/her name from old Ration card.

You can get the new application from your currently residing Taluk Supply Office.

Payback Points - How to redeem - How to merge multiple payback accounts - Block Payback card - Payback customer care

Joomla and Forum Integration - Integrating Forums to Joomla

Joomla is one of the most popular CMS opensource packages. It is very easier to develop website's using Joomla. You just need to download Joomla package from Joomla's Official website www.joomla.org  and install it on your domain and later adding customizations to templates and feature and Your website is ready :). Now a days most websites provides a forum section for it users for discussing various article topics, gathering opinions etc.



Following are some best know forum opensource packages which can be integrated with Joomla and create a new forum experience for users


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 »
Member
Search This Blog