What are the potential pitfalls of using file types to validate uploaded files in PHP?

One potential pitfall of using file types to validate uploaded files in PHP is that file extensions can be easily manipulated, leading to potential security vulnerabilities. To mitigate this risk, it is recommended to use file mime types for validation instead of relying solely on file extensions.

// Get the mime type of the uploaded file
$mime = mime_content_type($_FILES['file']['tmp_name']);

// Allowed mime types
$allowed_mimes = ['image/jpeg', 'image/png', 'application/pdf'];

// Check if the uploaded file's mime type is in the allowed list
if (in_array($mime, $allowed_mimes)) {
    // File is valid
    // Proceed with file processing
} else {
    // Invalid file type
    echo "Invalid file type. Please upload a valid file.";
}