What potential security risks are associated with relying on the client-provided file type in PHP file uploads?

Relying on the client-provided file type in PHP file uploads can pose a security risk known as file type spoofing. This occurs when a malicious user renames a file with a different extension to bypass file type validation checks and upload potentially harmful files. To mitigate this risk, it is recommended to validate the file type based on the file's content rather than relying solely on the client-provided file type.

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

if ($mime === 'image/jpeg' || $mime === 'image/png') {
    // Process the uploaded file
} else {
    // Invalid file type
    echo 'Invalid file type. Only JPEG and PNG files are allowed.';
}
finfo_close($finfo);