1

I need to use PHP to check if a file exists outside of there server it's running on. This question answers perfectly how to check for a file on the server:

PHP's file_exists() will not work for me?

...but I need to check a file on a different server on our network to the one that PHP is hosted on. Is this possible?

Community
  • 1
  • 1
Sinister Beard
  • 3,570
  • 12
  • 59
  • 95
  • Look at http://se1.php.net/file_exists and read the comment by "Fabrizio (staff at bibivu dot com)" who wrote a function for how to check if a URL exists. – Simon Forsberg Sep 20 '13 at 11:37
  • Not ever tried this, but could you possibly use a cURL to see if it exists (assuming it is a URL, or if not, you should add whether you are allowed to browse the file structure of the other server from your own. – Fluffeh Sep 20 '13 at 11:37
  • That depends of access protocol. With HTTP, there's `cURL` as an option, with sftp or ssh - it's `ssh2_`, with ftp it's `ftp_` – Alma Do Sep 20 '13 at 11:40
  • As of PHP 5.0.0, this function can also be used with some URL wrappers. See http://php.net/file_exists – Amal Murali Sep 20 '13 at 11:42
  • cURL is crashing my page - it's very old code that I'm editing it, so I assume the problem is at my end, but what could be causing that? – Sinister Beard Sep 20 '13 at 13:40

5 Answers5

2

Possible in an indirect way. You can always send a HEAD request and see if you are getting HTTP 200 back from the server for the requested resource. Hope it helps.

anupam
  • 756
  • 5
  • 11
  • But it will check if the url exists or not – Rohit Choudhary Sep 20 '13 at 11:39
  • It will not check for the URL. For example, the way the web browsers are working. How those show 404 pages to us? For each resource (e.g. CSS file, images, JS etc web browsers get HTTP response code (like 200, 401, 404, 500, 301, 302 etc). According to the response from the server, we see the rendered page with resources. Hope it helps. – anupam Sep 20 '13 at 11:43
2

Two possible solutions:

  1. Export the filesystem/directory in which the file resides on Server 2 so that it can be mounted on Server 1. You can for example use NFS or CIFS (SMB/Samba). Then you could use file_exists on the path where you have mounted the filesystem and it shall work as if it were a local file.

  2. Have some service running on Server 2 which can check the file existence. E.g. you could install a webserver on Server 2 as well and write a short PHP script which does the file_exists and then for example returns only 1 or 0 as text. You can then request that URL from Server 1 and process the result.

Juliane Holzt
  • 2,135
  • 15
  • 14
0

Try to use curl like,

$ch = curl_init("http://www.example.com/file.jpg");

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode > 400 -> not found, $retcode = 200, found.
curl_close($ch);
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0

You can try (only for small files!):

$file = file_get_contents('http://www.yourdomain.com/yourfile');
if (!empty($file)) {
    // there is a file
}
Adam
  • 1,371
  • 2
  • 11
  • 12
0

There is no way to check if file exists on server and be 100% that this is true. Because of file rights.

You can try to download the file and when it has right to be read then you can use functions like fopen() file_get_contents() etc.

You can also open connection on http port and check if answer is 200. If so then file exists.

If u want use fopen and file_get_contents you should check if allow_url_fopen is set to TRUE.

On file_exists() page in manual there is code:

 $file = 'http://www.domain.com/somefile.jpg';
 $file_headers = @get_headers($file);
 if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
     $exists = false;
 }
 else {
     $exists = true;
 }

You can use it too.

Robert
  • 19,800
  • 5
  • 55
  • 85