php method to get the file mime type
1. Use the mime_content_type method
string mime_content_type ( string $filename ) Returns the MIME content type for a file as determined by using information from the magic.mime file.
<?php $mime_type = mime_content_type('1.jpg'); echo $mime_type; // image/jpeg ?>
2. Use Fileinfo method (official recommendation)
Using fileinfo requires installing the php_fileinfo extension.
If it has been installed, it can be found in the extension_dir directory php_fileinfo.dll(windows), fileinfo.so(linux)
Open php.ini and change the " in front of extension=php_fileinfo.dll ;"Remove it and restart apache.
<?php $fi = new finfo(FILEINFO_MIME_TYPE); $mime_type = $fi->file('1.jpg'); echo $mime_type; // image/jpeg ?>
Using the exif_imagetype method requires installing the php_exif extension and the php_mbstring extension
If it has been installed, it can be in the extension_dir directory Find php_exif.dll(windows),exif.so(linux)
Open php.ini, remove the "," before extension=php_mbstring.dll, extension=php_exif.dll, and then restart apache
<?php $image = exif_imagetype('1.jpg'); $mime_type = image_type_to_mime_type($image); echo $mime_type; // image/jpeg ?>
The above introduces the method of obtaining the mime type of the file in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.