How can one check if a URL exists in PHP without downloading the entire file?
To check if a URL exists in PHP without downloading the entire file, you can use the `get_headers()` function to send a HEAD request to the URL and retrieve the headers. By checking the response code in the headers, you can determine if the URL exists without downloading the entire file.
function urlExists($url){
$headers = @get_headers($url);
if($headers && strpos($headers[0], '200')){
return true;
} else {
return false;
}
}
$url = "https://www.example.com";
if(urlExists($url)){
echo "URL exists";
} else {
echo "URL does not exist";
}
Keywords
Related Questions
- What are the recommended tools and software for safely editing and managing MySQL database files in a PHP development environment?
- How can escaping characters like quotes be used effectively in PHP code to prevent parsing errors?
- What alternatives to using PHP scripts for file downloads are recommended for server-side operations?