How can one efficiently determine if an image file exists on a remote server using PHP?
To efficiently determine if an image file exists on a remote server using PHP, you can use the `get_headers()` function to retrieve the headers of the image URL. If the response code is 200, then the image file exists; otherwise, it does not.
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.";
}
Keywords
Related Questions
- Can you send a database query within a while loop in PHP?
- How can If statements be parsed in PHP without using PHP within the HTML template?
- What are some efficient methods for handling text wrapping and line breaks in PHP when overlaying text on images, particularly when dealing with user-input text of varying lengths?