This article mainly introduces how to obtain the mime type of a file in php. The example analyzes various common techniques for obtaining the mime type of a file in php, which is of great practical value. For more information, see
This article mainly introduces how to obtain the mime type of a file in php. The example analyzes various common techniques for obtaining the mime type of a file in php, which is of great practical value. For more information, see
This example describes how to obtain the object mime type in php. Share it with you for your reference. The details are as follows:
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?>
However, this method has been deprecated since php5.3 or later. We recommend that you use the fileinfo method instead.
2. Use the Fileinfo method (officially recommended)
To use fileinfo, you must install the php_fileinfo extension.
If the package has been installed, you can find php_fileinfo.dll (windows), fileinfo. so (linux) in the extension_dir directory)
Open php. ini, remove ";" before extension = php_fileinfo.dll, and restart apache.
<? Php $ fi = new finfo (FILEINFO_MIME_TYPE); $ mime_type = $ fi-> file('1.jpg '); echo $ mime_type; // image/jpeg?>
3. Use the image_type_to_mime_type method (only image types can be processed)
To use the exif_imagetype method, you must install the php_exif extension and the php_mbstring extension.
If it has been installed, you can find php_exif.dll (windows), exif. so (linux) in the extension_dir directory)
Open php. ini, and "," remove, and restart apache "before extension = php_mbstring.dll and extension = php_exif.dll.
<? Php $ image = exif_imagetype('1.jpg '); $ mime_type = image_type_to_mime_type ($ image); echo $ mime_type; // image/jpeg?>
Tips: if you use the suffix of the file name to determine whether the file suffix can be modified, it is inaccurate to use the suffix.
I hope this article will help you with php programming.