What are some best practices for validating file types in PHP upload scripts?

When creating PHP upload scripts, it is crucial to validate the file types being uploaded to prevent malicious files from being executed on the server. One best practice is to check the file's MIME type using the `$_FILES['file']['type']` variable. Additionally, you can use file extension checks to further validate the file type.

// Validate file type by MIME type
$allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($_FILES['file']['type'], $allowedMimeTypes)) {
    die('Invalid file type. Only JPG, PNG, and GIF files are allowed.');
}

// Validate file type by file extension
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
$uploadedExtension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if (!in_array($uploadedExtension, $allowedExtensions)) {
    die('Invalid file type. Only JPG, PNG, and GIF files are allowed.');
}