Last active
March 1, 2021 16:01
-
-
Save vyspiansky/10959566 to your computer and use it in GitHub Desktop.
PHP: file_exists for remote URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
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
To check if
allow_url_fopen
is enabled or not you can useini_get()
:Or just print the
get_headers
:and you should see something like:
Be careful, the message may have the different version of the protocol: "HTTP/1.0 404 Not Found" or "HTTP/1.1 404 Not Found" etc.