How does php obtain the object mime type? The MIME type is the type used by an application to open a file with a certain extension. when a file with the extension is accessed, the browser automatically opens the file with the specified application. It is used to specify custom client file names and open media files. Php: how to obtain the mime type of a file. php: how to obtain the mime type of a file.
? The MIME type is the type used by an application to open a file with a certain extension. when a file with the extension is accessed, the browser automatically opens the file with the specified application. It is used to specify custom client file names and open media files.
Php provides the following methods to obtain the object mime type:
1. use the mime_content_type () function to determine whether to obtain the mime type.
? Mime_content_type: the MIME type of the specified object is returned,
Usage:
Echomime_content_type('php.gif '). "\ n ";
Echomime_content_type ('test. php ');
Output:
However, php 5.3.0 has removed this function. If you still want to use this function, you can configure php to enable magic_mime extension .?
2. php Fileinfo obtains the object MIME type (finfo_open)
? The alternative function of mime_content_type () officially recommended by PHP is the Fileinfo function. PHP 5.3.0 + supports the Fileinfo support-enabled function by default. you can use finfo_open () to determine the object 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 () to obtain the image MIME type
? If you need to determine that only image files are MIME-type files, you can first use the exif_imagetype () function to obtain the image type constant, and then use image_type_to_mime_type () the function converts an image type constant to the MIME type of an image file.
Note: You need to configure php_mbstring.dll (required for Windows) and extension = php_exif.dll in php. ini.
Image_type_to_mime_type () to obtain the image MIME type
If we need to determine that only image files are MIME-type files, we can first use the exif_imagetype () function to obtain the image type constant, and then use image_type_to_mime_type () the function converts an image type constant to the MIME type of an image file. In php. ini, configure php_mbstring.dll (required for Windows) and extension = php_exif.dll. Phpinfo ()"-Enable-exif". First, exif_imagetype returns the image type constant (Imagetype Constants), as shown inIMAGETYPE_GIF,IMAGETYPE_JPEG,IMAGETYPE_PNG.
4. php uploads a file to obtain the MIME type
If you use php to upload FILES and check the MIME type of the uploaded FILES, you can use the global variable $ _ FILES ['uploadfile'] ['type']. the client browser detects and obtains the file MIME type.