This article mainly introduces you to determine the PHP file type code, the need for friends can refer to
What is a MIME type, which is a type of file that sets an extension of an application to be opened by an app, and when the extension file is accessed, the browser automatically opens with the specified application.
Many are used to specify some client-customized file names, as well as some ways to open media files.
Reference Link: PHP file format (MIME type) comparison table.
1, Mime_content_type () function to determine the MIME type obtained
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 the magic_mime extension.
2. PHP Fileinfo Get file MIME type (finfo_open)
PHP official recommendation The alternative function of Mime_content_type () is the FileInfo function. PHP 5.3.0+ already supports the FileInfo function (FileInfo support-enabled) By default, and you can use Finfo_open () to determine the file MIME type without having to make any configuration. Usage:
$finfo = Finfo_open (fileinfo_mime), $mimetype = Finfo_file ($finfo, $filename); Finfo_close ($finfo);
3, Image_type_to_mime_type () Get the picture MIME type
If you need to determine the MIME type of files only image files, then you can first use the Exif_imagetype () function to get the image type constants, and then use the Image_type_to_mime_type () function to convert the image type constants 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 upload a file using PHP to 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. Determine file type by file extension
<?php $filename = "D:\\296.mid"; $file = fopen ($filename, "RB"); $bin = Fread ($ file, 2); Read-only 2-byte 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 = ' ra R '; 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;?;