What potential pitfalls can arise when relying solely on file extensions for file type validation in PHP?

Relying solely on file extensions for file type validation in PHP is not secure because file extensions can be easily manipulated. It is important to validate the file type based on its actual content rather than just the extension to prevent malicious files from being uploaded to the server.

// Validate file type based on content rather than extension
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $_FILES['file']['tmp_name']);

if ($mime_type === 'image/jpeg' || $mime_type === 'image/png') {
    // File type is valid, proceed with upload
} else {
    // File type is not allowed
    echo 'Invalid file type. Only JPEG and PNG files are allowed.';
}
finfo_close($finfo);