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;
Related Questions
- What potential issues can arise when moving a PHP project from a local environment to a server with php5-fpm + nginx installed?
- What are the best practices for creating a search function in PHP that efficiently searches through a large number of HTML pages?
- How can PHP be used to create an image for a forum signature?