How does the "finfo" class work in PHP for image handling and validation?
The "finfo" class in PHP can be used to determine the file type of an image file, which can be useful for image handling and validation. To use the "finfo" class, you need to create an instance of the class and then use the "file" method to get information about the file. This information can include the MIME type of the file, which can be used to validate that the file is actually an image file.
// Create a new finfo object
$finfo = new finfo(FILEINFO_MIME_TYPE);
// Get the MIME type of the file
$mime_type = $finfo->file($file_path);
// Check if the MIME type is an image type
if (strpos($mime_type, 'image') !== false) {
// File is an image
echo 'File is an image';
} else {
// File is not an image
echo 'File is not an image';
}