What is the MIME type, which is the type of file that sets an extension to open with an application, and when the file is accessed, the browser automatically opens with the specified application.
Used to specify some client-defined file names and how some media files are opened.
Reference Link: PHP file format (MIME type) comparison table.
1, Mime_content_type () function to judge get MIME type
Mime_content_type returns the MIME type of the specified file, using:
echo mime_content_type (' Php.gif '). "\ n";
echo mime_content_type (' test.php ');
Output:
Image/gif
Text/plain
But PHP 5.3.0 has discarded the function. If you still want to use this function, you can configure PHP to enable Magic_mime extensions.
2, PHP Fileinfo get file MIME type (finfo_open)
An alternative function of the official PHP recommendation Mime_content_type () is the FileInfo function. The PHP 5.3.0+ already supports the FileInfo function (FileInfo support-enabled) By default and can use Finfo_open () to determine the file MIME type without any configuration. Usage:
$finfo = Finfo_open (fileinfo_mime);
$mimetype = Finfo_file ($finfo, $filename);
Finfo_close ($finfo);
3, Image_type_to_mime_type () Get picture MIME type
If you need to determine a MIME-type file with only an image file, you can first use the Exif_imagetype () function to get the image type constant, and then use the Image_type_to_mime_type () function to convert the image type constant to the MIME type of the picture file.
Note: You need to configure open Php_mbstring.dll (Windows required) and Extension=php_exif.dll in php.ini.
4, PHP upload file to get MIME type
If you use PHP to upload a file and detect the MIME type of the uploaded file, you can use the global variable $_files[' uploadfile ' [' type '], which is detected by the client's browser to get the file MIME type.
5, by file extension to determine the file type
<?php
$filename = "D:\\296.mid";
$file = fopen ($filename, "RB");
$bin = Fread ($file, 2); Read Only 2 bytes
fclose ($file);
$strInfo = @unpack ("C2chars", $bin);
$typeCode = Intval ($strInfo [' chars1 ']. $strInfo [' chars2 ']);
$fileType = ';
Switch ($typeCode)
{case
7790:
$fileType = ' exe ';
break;
Case 7784:
$fileType = ' midi ';
break;
Case 8297:
$fileType = ' rar ';
break;
Case 255216:
$fileType = ' jpg ';
break;
Case 7173:
$fileType = ' gif ';
break;
Case 6677:
$fileType = ' bmp ';
break;
Case 13780:
$fileType = ' png ';
break;
Default:
echo ' unknown ';
}
Echo ' This is a '. $fileType. ' File: '. $typeCode;
The above is the PHP file type to judge several methods, if you have a better way, you can leave a message