Hello,
How do I check if an URL exists and not 404 in PHP
Thanks Jean
Hello,
How do I check if an URL exists and not 404 in PHP
Thanks Jean
Here:
$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;
}
From here: http://www.php.net/manual/en/function.file-exists.php#75064
...and right below the above post, there's a curl solution:
function url_exists($url) {
if (!$fp = curl_init($url)) return false;
return true;
}
$f1 = 'http://www.sample.com/file.jpg'; $file_headers = @get_headers($f1); if($file_headers[0] == 'HTTP/1.1 404 Not Found') { $exists = false; } else { $exists = true; }
Careful, get_headers("http://www.domain.com/somefile.jpg") can be very slow (1 minute)