What are the potential security risks associated with relying solely on MIME types for image file uploads in PHP?

Relying solely on MIME types for image file uploads in PHP can be risky as MIME types can be easily spoofed or manipulated by an attacker. To enhance security, it is recommended to validate the file extension along with the MIME type to ensure that the uploaded file is indeed an image.

// Validate the file extension along with the MIME type
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
$uploadedFile = $_FILES['file']['tmp_name'];
$uploadedExtension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$uploadedMimeType = mime_content_type($uploadedFile);

if (in_array($uploadedExtension, $allowedExtensions) && strpos($uploadedMimeType, 'image/') === 0) {
    // File is a valid image
    // Proceed with file upload
} else {
    // Invalid file type
    echo "Invalid file type. Please upload an image file.";
}