Echelon
Echelon

Reputation: 29

PHP script to check on remote server, a file exists

I am having roblems with locating a PHP script to allow me to obtain the contents of a txt file on a remote server, then output to a variable. Outputting something to a variable is not the hard part. It's the picking up and reading the contents of the file that's the hard part. Anyone have any ideas?

I have trawled the forum and can only locate a method that works locally. Not ideal as the target is remote.

The objective really is, how do I find out if a file exists on the remote server and output a status in html.

Ideas?

Upvotes: 2

Views: 9819

Answers (5)

Danny Pryor
Danny Pryor

Reputation: 1

I know this is an old thread, but as Lars Ebert points out, checking for the existence of a file on a remote server can be tricky, so checking the server response, using cURL, is how I have been able to do it on our big travel site. Using file_exists() threw an error every time, but checking for a "200 OK" has proved quite successful. Here is the code we are using to check for images for our hotel listings page:

$media_url = curl_init("http://pathto/remote_file.png");
curl_setopt($media_url, CURLOPT_RETURNTRANSFER, true);
$media_img = curl_exec($media_url);
$server_response = curl_getinfo($media_url, CURLINFO_HTTP_CODE);
if($server_response != 200){
    echo "pathto/graphics/backup_image.png";
}else{
    echo "http://pathto/remote_file.png";
}

Where "http://pathto/remote_file.png" is the remote image we seek, but we need to know whether it is really there. And "pathto/graphics/backup_image.png" is what we display if the remote image does not exist.

I know it's awfully verbose, compared to file_exists(), but it's also more accurate, at least so far.

Upvotes: 0

Alberto Miola
Alberto Miola

Reputation: 4751

You can try to use this code:

if (file_exists($path)) {
    echo "it exists";
} else {
    echo "it does not exist";
}

As you can see $path is the path of your file. Of course you can write anything else instead of those echo.

Upvotes: 3

user1864610
user1864610

Reputation:

Assuming your remote server is accessible by http or ftp you can use file_exists():

if (file_exists("http://www.example.com/somefile.txt")) {
 echo "Found it!;
}

or

if (file_exists("ftp:user:[email protected]/somefile.txt")) {
 echo "Found it!;
}

Upvotes: 4

Lars Ebert-Harlaar
Lars Ebert-Harlaar

Reputation: 3537

Accessing files on other servers can be quite tricky! If you have access to the file via ftp, you can use ftp to fetch the file, for example with ftp_fget().

If you do not have access to the file-system via ssh, you only can check the response the server gives when requesting the file. If the server responds with an error 404, the file is either not existent or it is not accessible via http due to the server configuration.

You can check this through curl, see this tutorial for a detailled explanation of obtaining the response code through curl.

Upvotes: 2

Amal
Amal

Reputation: 76646

Use this:

$url = 'http://php.net';
$file_headers = @get_headers($url);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    echo "URL does not exist";
}
else {
    echo "URL exists";
}

Source: http://www.php.net/manual/en/function.file-exists.php#75064

Upvotes: 3

Related Questions