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

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

Your SBI Debit card ending with XX0000 is deactivated only for Internet txn.

SBI account holders may have received an SMS with following message, supposed to be from State Bank of India (SBI).

Your SBI Debit card ending with XX0000  is deactivated only for Internet txn. To activate send SMS "SWON ECOM 0000" to 09223966666. No change for ATM/POS usage
** Replace the four Zeros with last 4 digits of your debit card number

Recently many of the SBI account holder has losed their money due to a hi-tech ATM robbery which happened in Thiruvananthapuram, capital city of Kerala.

How to turn off BSNL Buzz services - Steps to deactivate BSNL Buzz messages

BSNL Buzz is a service from BSNL for mobile customers, in which when your phone is in idle mode, teasers or small buzz messages related to topics like entertainment, lifestyle, sports, business, fun, polls, daily news updates are delivered to your mobile. When you activate a BSNL SIM card, through which BSNL BUZZ is available, you automatically accept the terms and conditions for teaser delivery.

Recently, I bought a new BSNL SIM card. After the activation, when ever I unlock the phone, I was getting lots of buzz message one after another. I have to click on cancel / back button multiple time to view the home screen. Some times when some call arrives, it was difficult to attend calls due to this teaser popups. So I decided to deactivate the BSNL buzz. Here are the steps to Deactivate or Turn Off BSNL Buzz.


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