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 use WiFi adapter on Ubuntu 16.04 desktop PC - Realtek RTL8188EUS 802.11n Wireless USB Network Adapter Driver installation

Installation of Realtek RTL8188EUS 802.11n Wireless USB Network Adapter on Desktop PC having Ubuntu 16.04 OS My PC is running in Ubuntu 16.04 OS, recently I thought of using a dongle wife adapter to access our home's Wifi network. For this I used Realtek RTL8188EUS 802.11n Wireless USB Network Adapter ( Model No: OT-WUA950NM ) This small device cost around Rupees 250/- to Rs 300/- in India. I did the following steps for installation of this Realtek Nano Wifi Adapter: Plug Realtek RTL8188EUS 802.11n Wireless USB Network Adapter to your PC's USB port, Take the terminal application and run the command "lsusb" to list the plugged in usb devices: Myhome:~$ lsusb Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 001 Device 003: ID 0bda:8179 Realtek Semiconductor Corp. R...

How to delete videos from your Youtube Watch History list?

How to Delete Individual or all videos from your Youtube Watch History list? Youtube keeps a fine record of the videos that you had watched earlier. You can view this by visiting the History section. If you want to remove the video's from the list do the following: Logon to Youtube and click on the "History" tab on the left menu to view Watch History ( Read more ) There will be check boxes corresponding to each video in the list Tick the check boxes of the videos which you want to remove Click on " Remove " button to delete the videos.

ICICI prudential Customer portal updated - Option to change password is missing - Know how to change your ICICI prudential password

Recently I received an SMS from ICICI prudential asking for login to their website's customer portal using the phone number as user Id and an autogenerated one time password given in the message as password. The SMS messsage was like this. Dear ***Cust Name*** login to your policy(ies) on www.iciciprulife.com with your user id as **mobile number*** and One time use password as ***password***


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