Several ways to get file MIME types in PHP
? A MIME type is a type of file that sets an extension that is opened by an application, and the browser automatically opens with the specified application when the extension file is accessed. Many are used to specify some client-customized file names, as well as some ways to open media files.
PHP Gets the file MIME type in several ways:
1, Mime_content_type () function to determine the MIME type obtained
? Mime_content_type returns the MIME type of the specified file,
usage:
echo Mime_content_type ( ' Php.gif ' Span style= "color: #007700;" >) . echo mime_content_type ( ' test.php ' );
output:
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.
Image_type_to_mime_type () Get picture MIME type
If we need to determine that MIME-type files are only image files, 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. The same php.ini to be configured to open Php_mbstring.dll (Windows requires) and Extension=php_exif.dll. Phpinfo () "–enable-exif". First Exif_imagetype returns the image type constant (Imagetype Constants), such as imagetype_gif,imagetype_jpeg,imagetype_ PNG and so on.
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.