Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active March 1, 2021 16:01
Show Gist options
  • Select an option

  • Save vyspiansky/10959566 to your computer and use it in GitHub Desktop.

Select an option

Save vyspiansky/10959566 to your computer and use it in GitHub Desktop.
PHP: file_exists for remote URL
<?php
// More details here:
// https://vyspiansky.github.io/2017/11/09/remote-file-exists-using-get-headers/
$file_headers = @get_headers($url);
if ($file_headers[0] == 'HTTP/1.0 404 Not Found'){ // or "HTTP/1.1 404 Not Found" etc.
$file_exists = false;
} else {
$file_exists = true;
}
@CycoCE
Copy link

CycoCE commented Aug 14, 2020

I use the following so that I do not have to worry about HTTP/1.0 or 1.1, etc.

if(strpos($file_headers[0], '404') !== false)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment