What is the function of getimagesize() in PHP and how can it be used to check if a file is an image?

The getimagesize() function in PHP is used to get the size and MIME type of an image file. This function can be used to check if a file is an image by attempting to retrieve the image size information. If the function returns an array with image size information, then the file is likely an image. If the function returns false, then the file is not an image.

$filename = 'example.jpg';

$image_info = getimagesize($filename);

if($image_info !== false) {
    echo 'The file is an image.';
} else {
    echo 'The file is not an image.';
}