Verifying URL Availability Using PHP
If you need to ascertain the existence of a URL without triggering a 404 error in PHP, the following strategies are at your disposal:
Method 1: Using get_headers()
$file = 'http://www.example.com/somefile.jpg'; $file_headers = @get_headers($file); if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') { $exists = false; } else { $exists = true; }
Method 2: Utilizing cURL
function url_exists($url) { return curl_init($url) !== false; }
These methods allow you to determine whether a URL is accessible or not, safeguarding your application from returning undesirable 404 errors.
The above is the detailed content of How to Verify URL Availability in PHP Without Triggering 404 Errors?. For more information, please follow other related articles on the PHP Chinese website!