What is the most efficient way to check if an image exists on a remote server in PHP?
When checking if an image exists on a remote server in PHP, the most efficient way is to use the `get_headers()` function to retrieve the headers of the image URL. By checking the response code in the headers, we can determine if the image exists (200 OK) or not (404 Not Found).
function imageExists($url) {
$headers = get_headers($url);
return stripos($headers[0], "200 OK") ? true : false;
}
// Example usage
$imageUrl = "https://example.com/image.jpg";
if(imageExists($imageUrl)) {
echo "Image exists!";
} else {
echo "Image does not exist.";
}