How can PHP scripts handle file type validation effectively when uploading images?

When uploading images using PHP scripts, it is important to validate the file type to ensure that only images are accepted. This can be done by checking the MIME type of the uploaded file using the `$_FILES` superglobal array. By comparing the MIME type against a list of allowed image types, you can effectively handle file type validation.

// Define an array of allowed image MIME types
$allowedTypes = array('image/jpeg', 'image/png', 'image/gif');

// Get the MIME type of the uploaded file
$uploadedFileType = $_FILES['file']['type'];

// Check if the uploaded file type is in the list of allowed types
if (in_array($uploadedFileType, $allowedTypes)) {
    // File type is valid, proceed with file upload
    // Your upload code here
} else {
    // File type is not allowed, display an error message
    echo 'Invalid file type. Only JPEG, PNG, and GIF images are allowed.';
}