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

Strange problem occured while trying to create a CSV file using PHP Script - The file is not seen on FTP but can download using file's absolute path url

Strange problem occured while trying to create a CSV file - The file is not seen on FTP but can download using file's absolute path url Last day I came across a strange problem when I tried to create a csv file on therver using a PHP script. the script was simply writing a given content as a csv file. The file will be created runtime. What happened was, The script executed fine, file handler for new file was created and contents was wrote into the file using fwrite and it returned the number of bytes that was written.

How to get the Query string of a URL using the Javascript (JS)?

JS function get the Query string of a URL or value of each parameter using the Javascript(JS)? If you want to get your current page's url var my_url=document.location; to get the query string part of the url use like this: var my_qry_str= location.search; this will return the part of the url starting from "?" following by query string Lets assume that your current page url is http://www.crozoom.com/2013/page.html?qry1=A&qry2=B then the location.search function will return " ?qry1=A&qry2=B " to exclue "?", do like this:


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