What are the potential consequences of not properly validating MIME types in PHP file uploads?

If MIME types are not properly validated in PHP file uploads, it can lead to security vulnerabilities such as allowing malicious files to be uploaded and executed on the server. To prevent this, it is essential to validate the MIME type of the uploaded file against a whitelist of allowed types before processing or storing the file.

// Validate MIME type of uploaded file
$allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif'];
$uploadedFileMimeType = mime_content_type($_FILES['file']['tmp_name']);

if (!in_array($uploadedFileMimeType, $allowedMimeTypes)) {
    // Invalid MIME type, handle error or reject file upload
    die('Invalid file type. Allowed types are: ' . implode(', ', $allowedMimeTypes));
}

// Process or store the uploaded file
// Your code here...