What are the potential pitfalls of using fsockopen() to check for the existence of an image?
One potential pitfall of using fsockopen() to check for the existence of an image is that it may not handle certain image formats correctly, leading to false positives or false negatives. To solve this issue, it is recommended to use the getimagesize() function in PHP, which can accurately determine the dimensions and type of an image file without relying on network requests.
// Check if an image file exists using getimagesize()
function checkImageExists($url) {
$image_info = @getimagesize($url);
return $image_info !== false;
}
// Usage example
$image_url = 'https://example.com/image.jpg';
if (checkImageExists($image_url)) {
echo 'Image exists.';
} else {
echo 'Image does not exist.';
}