64

I need to check if a particular file exists on a remote server. Using is_file() and file_exists() doesn't work. Any ideas how to do this quickly and easily?

1

8 Answers 8

116

You don't need CURL for that... Too much overhead for just wanting to check if a file exists or not...

Use PHP's get_header.

$headers=get_headers($url);

Then check if $result[0] contains 200 OK (which means the file is there)

A function to check if a URL works could be this:

function UR_exists($url){
   $headers=get_headers($url);
   return stripos($headers[0],"200 OK")?true:false;
}

/* You can test a URL like this (sample) */
if(UR_exists("http://www.amazingjokes.com/"))
   echo "This page exists";
else
   echo "This page does not exist";
6
  • 3
    By default get_headers uses a GET request to fetch the headers - so instead of the overhead of a curl request (not really sure what overhead is being referred to by that) - there's a wasteful GET request which drops the body - instead of using a HEAD request and only receiving the headers.
    – AD7six
    Jul 3, 2015 at 18:36
  • 3
    @AD7six: I was assuming setting up cURL in memory would cause overload, I did some testing comparing the two methods and you're right: if you have the cURL library loaded it's consistently faster to use the accepted method compared to get_headers. I compared all 3 mentioned methods: cURL is the fastest, then get_headers, then getimagesize with the added downside getimagesize will only tell you if an image exists. It is what was asked, so it's still a valid answer here, but it's not very versatile.
    – patrick
    Jul 6, 2015 at 15:19
  • 1
    @ad7six are you sure get_headers is requesting the body? fyi, you can override the GET request via stream context: stream_context_set_default(['http' => ['method' => 'HEAD']]); $headers = get_headers('example.com');
    – Tobi
    Jan 29, 2016 at 15:59
  • answer is old but very useful Jan 15, 2019 at 3:09
  • Example of using a HEAD request, as mentioned by @AD7six: php.net/manual/en/function.get-headers.php
    – Genki
    Jul 5, 2020 at 4:08
85

You have to use CURL

function does_url_exists($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($code == 200) {
        $status = true;
    } else {
        $status = false;
    }
    curl_close($ch);
    return $status;
}
4
  • You don't need CURL for that... way too much overhead... check my answer below (use PHP's get_headers for this!)
    – patrick
    Apr 18, 2015 at 8:37
  • 1
    like the others it's too, not checking whether it's a file or not, just checking the URL. echo is_url_exists('http://stackoverflow.com') ? 'Yes' : 'No'; echoed Yes. Aug 10, 2015 at 8:14
  • this was not working for me until I added the CURLOPT_FOLLOWLOCATION,true directive Jul 4, 2017 at 17:07
  • The function name should be does_url_exists ;)
    – JuliSmz
    Jul 3, 2019 at 17:06
21

I've just found this solution:

if(@getimagesize($remoteImageURL)){
    //image exists!
}else{
    //image does not exist.
}

Source: http://www.dreamincode.net/forums/topic/11197-checking-if-file-exists-on-remote-server/

7
  • Did you mean getimagesize? As here: uk1.php.net/getimagesize if so then the docs say it can reference local or remote files.
    – Todd
    Jan 26, 2014 at 18:41
  • 3
    getimagesize is way too much overhead. If you just want to know if a file exists use PHP's get_header (check my answer)
    – patrick
    Apr 18, 2015 at 8:39
  • It is not much bad ,but it is just worked for images!
    – ganji
    Sep 13, 2015 at 14:59
  • I agree with patrick this is not a solution, I hope not too many ppl used this method.
    – ekerner
    Jan 16, 2016 at 2:22
  • 1
    @herbert. All depends on how many times this will be called and how many times people will be using this...
    – patrick
    Jul 2, 2016 at 1:30
13

Hi according to our test between 2 different servers the results are as follows:

using curl for checking 10 .png files (each about 5 mb) was on average 5.7 secs. using header check for the same thing took average of 7.8 seconds!

So in our test curl was much faster if you have to check larger files!

our curl function is:

function remote_file_exists($url){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if( $httpCode == 200 ){return true;}
    return false;
}

here is our header check sample:

function UR_exists($url){
   $headers=get_headers($url);
   return stripos($headers[0],"200 OK")?true:false;
}
0
2

You can use the function file_get_contents();

if(file_get_contents('https://example.com/example.txt')) {
    //File exists
}
3
  • 4
    It will slow down your application and increase TTFB ( Time To Take First Byte ). So Never use this function. Jun 18, 2019 at 12:07
  • 3
    it takes all content of file ... if you want to check large files , it will get too time Oct 18, 2019 at 14:56
  • It will also break your page if url is wrong.. I mean no file exist on a given path. Jul 2, 2021 at 13:44
1

Do a request with curl and see if it returns a 404 status code. Do the request using the HEAD request method so it only returns the headers without a body.

0
$file = 'https://picsum.photos/200/300';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
} 
-1
    $headers = get_headers((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER[HTTP_HOST] . '/uploads/' . $MAIN['id'] . '.pdf');
    $fileExist = (stripos($headers[0], "200 OK") ? true : false);
    if ($fileExist) {
    ?>
    <a class="button" href="/uploads/<?= $MAIN['id'] ?>.pdf" download>скачать</a> 
    <? }
    ?>
1
  • 3
    Welcome to Stack Overflow! Please see How to Answer. Always remember when answering a question you're not only answering to the OP, but also to future readers especially when answering 9 year old question. Thus, please edit the post to contain an explanation as to why this code works.
    – Adriaan
    May 26, 2020 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.