How can PHP be used to determine the MIME type of a file for validation purposes?

When validating file uploads in PHP, it is important to verify the MIME type of the file to ensure it is the expected type and prevent potential security risks. PHP provides the `finfo_open()` function to determine the MIME type of a file based on its content. By using this function, you can accurately validate the file type before processing it further.

// Open fileinfo resource
$finfo = finfo_open(FILEINFO_MIME_TYPE);

// Get MIME type of the uploaded file
$mime = finfo_file($finfo, $_FILES['file']['tmp_name']);

// Close fileinfo resource
finfo_close($finfo);

// Check if the MIME type is allowed
$allowed_types = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($mime, $allowed_types)) {
    echo "Invalid file type. Please upload a JPEG, PNG, or GIF file.";
    exit;
}

// Continue processing the file
// Your code here