How can the MIME type of a file be determined in PHP without using $_FILES['upload']['type']?

When uploading files in PHP, relying solely on $_FILES['upload']['type'] to determine the MIME type of a file can be unreliable as it can be easily manipulated by the user. To accurately determine the MIME type of a file, we can use the fileinfo extension in PHP. This extension allows us to get the actual MIME type of a file by examining its contents, providing a more secure and accurate method of identifying file types.

$file = $_FILES['upload']['tmp_name'];

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $file);
finfo_close($finfo);

echo "MIME type of the uploaded file: " . $mime_type;