3

Checking file exists on external server using file_get_contents() method, does this method will work properly?

$url_file = "http://website.com/dir/filename.php";
$contents = file_get_contents($url_file);

if($contents){
echo "File Exists!";
} else {
echo "File Doesn't Exists!";
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Ajie Kurniyawan
  • 393
  • 2
  • 18
  • No, you cannot check for file existence. URLs do not reference files, just resources (which may or not originate from actual files). You can check for returned content, or [`$http_response_header`](http://php.net/manual/en/reserved.variables.httpresponseheader.php) states though. And you could concretise how you concluded your code does not work, or what you're really trying to accomplish. – mario Aug 18 '14 at 01:33
  • 1
    that will check if the file has a content or not. You can try other ways to test if the file exits testing just the headers (check HTTP error codes): http://php.net/manual/en/function.get-headers.php – lepe Aug 18 '14 at 01:34
  • @lepe: I have control for that files, I can set file has a content or not.. so can I using this method? – Ajie Kurniyawan Aug 18 '14 at 01:59
  • @Ajie Kurniyawan: Yes it could work depending on your web server settings. The web server may respond with HTML:

    Error 404: Not found

    , for example, so it may have a content, but it may not be what you are expecting. I think its easier to check the headers, don't you think?
    – lepe Aug 18 '14 at 03:35

3 Answers3

4

I think best method for me is using this script:

$file = "http://website.com/dir/filename.php";
$file_headers = get_headers($file);

If file not exists output for $file_headers[0] is: HTTP/1.0 404 Not Found or HTTP/1.1 404 Not Found

Use strpos method to check 404 string if file doesn't exists:

if(strpos($file_headers[0], '404') !== false){
echo "File Doesn't Exists!";
} else {
echo "File Exists!";
}

Thanks for all help :)

Ajie Kurniyawan
  • 393
  • 2
  • 18
1

This would work for what you are looking to do. As seen here http://php.net/manual/en/function.file-exists.php#75064

$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;
}
blamonet
  • 592
  • 1
  • 4
  • 13
  • In my test if file not exists $file_headers[0] get value = 'HTTP/1.0 404 Not Found', so this method not works properly, should I change 1.1 to 1.0? – Ajie Kurniyawan Aug 18 '14 at 01:54
  • This all depends on the web server. Here is a good article on this: http://stackoverflow.com/questions/2769371/404-header-http-1-0-or-1-1 For me personally, I'd probably do some string manipulations to find 404 in the string and ignore the rest of the string so it would work in both cases. – blamonet Aug 18 '14 at 01:59
  • If I add this script maybe this method will works? $string = $file_headers[0]; $404 = "404"; $pos = strpos($string, $404); if ($pos === false) { echo "File Exists!"; } else { echo "File Doesn't Exists!"; } – Ajie Kurniyawan Aug 18 '14 at 02:09
  • @Ajie Kurniyawan: close, don't name your variable: $404, name it for example: $code or use "404" without a variable. (numeric variables are not allowed in PHP). Fixing that, it should work. – lepe Aug 18 '14 at 03:31
  • @lepe: thanks, I have created an answer for my own question, I think best way is that method.. thanks all for help :) – Ajie Kurniyawan Aug 18 '14 at 04:02
0

Both answers from blamonet and Ajie Kurniyawan are somewhat correct but there are more failures than 404(3xx, 4xx, 5xx responses). There are a lot of HTTP responses that would fail to "get/download" a file from a given server.

So I would suggest to check if 200 OK response.

$file = "http://website.com/dir/filename.php";
$file_headers = @get_headers($file);

if ($file_headers) {
  if (strpos($file_headers[0], ' 200 OK') === true) {
    echo "File Exists";
  } else {
    echo "File Doesn't Exist, Access Denied, URL Moved etc";
    //you can check more responses here 404, 403, 301, 302 etc 
  }
} else {
  echo "Server has not responded. No headers or file to show.";
}
fat_mike
  • 877
  • 12
  • 27