2

Is there a better way to check whether a file exists (it is on different domain so file_exists won't work) than this?

$fp = fsockopen($fileUri, 80, $errno, $errstr, 30);
if (!$fp) {
    // file exists
}
fclose($fp);
hakre
  • 193,403
  • 52
  • 435
  • 836
Richard Knop
  • 81,041
  • 149
  • 392
  • 552

6 Answers6

13

I do like this. It work always fine:

$url = "http://www.example.com/index.php";
$header_response = get_headers($url, 1);
if ( strpos( $header_response[0], "404" ) !== false )
{
  // FILE DOES NOT EXIST
}
else
{
  // FILE EXISTS!!
}
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
  • get_headers is slower than curl I think. – RobertPitt Nov 04 '10 at 09:44
  • curl libraries ahve proven to be more responsive and faster transferring data then regular php functions, which is why i think that get_headers would be slower. i have no proof, just what i have read in places, forums etc. – RobertPitt Nov 04 '10 at 09:58
  • 4
    I'd consider changing line 3 to `if ( strpos( $header_response[0], "200" ) === false )` so that 403 access-denied files aren't returned as available. – PaulSkinner Aug 28 '12 at 09:26
6

see this example and the explanations

$url = "http://www.example.com/index.php";
$header_response = get_headers($url, 1);
if ( strpos( $header_response[0], "404" ) !== false )
{
  // FILE DOES NOT EXIST
} 
else 
{
  // FILE EXISTS!!
}

or

file_get_contents("http://example.com/path/to/image.gif",0,null,0,1);

set maxlength to 1

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
3

You could use curl and check the headers for a response code.

This question has a few examples you could use.

When using curl, use curl_setopt to switch CURLOPT_NOBODY to true so that it only downloads the headers and not the full file. E.g. curl_setopt($ch, CURLOPT_NOBODY, true);

Community
  • 1
  • 1
drew
  • 1,312
  • 8
  • 20
3

From http://www.php.net/manual/en/function.fopen.php#98128

function http_file_exists($url)
{
    $f=@fopen($url,"r");
    if($f)
    {
        fclose($f);
        return true;
    }
    return false;
}

All my tests show that it works as expected.

partoa
  • 900
  • 13
  • 22
3

I would use curl to check the headers for this and validate the content type.

Something like:

function ExternalFileExists($location,$misc_content_type = false)
{
    $curl = curl_init($location);
    curl_setopt($curl,CURLOPT_NOBODY,true);
    curl_setopt($curl,CURLOPT_HEADER,true);
    curl_exec($curl);

    $info = curl_getinfo($curl);

    if((int)$info['http_code'] >= 200 && (int)$info['http_code'] <= 206)
    {
        //Response says ok.
        if($misc_content_type !== false)
        {
             return strpos($info['content_type'],$misc_content_type);
        }
        return true;
    }
    return false;
}

And then you can use like so:

if(ExternalFileExists('http://server.com/file.avi','video'))
{

}

or if your unsure about the extension then like so:

if(ExternalFileExists('http://server.com/file.ext'))
{

}
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • Thanks. I have used Alexander's solution for now (it's just one place in my app) but in case I need to check for external files in several places in my application I will probably use your function. – Richard Knop Nov 04 '10 at 11:20
  • Your best of beuilding a curl library that can send(get/post) recive(headers/html/files) and also interact with `DOMDocument`. This way you have everything you need within 1 lib. – RobertPitt Nov 04 '10 at 11:23
0

what about

<?php
      $a = file_get_contents('http://mydomain.com/test.html');
      if ($a) echo('exists'); else echo('not exists');
heximal
  • 10,327
  • 5
  • 46
  • 69