Php provides four methods to obtain the object mime type.

Source: Internet
Author: User
Php provides four methods to obtain the object mime type.
Php can obtain the mime type of a file. for example, you can use the mime_content_type () function to determine whether to obtain the mime type or Fileinfo to obtain the object MIME type (finfo_open). If you need it, refer.

What is the MIME type? it is a type of method that allows 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. Reference: PHP file format (mime type) table. 1. the mime_content_type () function determines whether to obtain the mime type mime_content_type and returns the MIME type of the specified file. usage:

echo mime_content_type ( 'php.gif' ) . "\n" ;echo mime_content_type ( 'test.php' );

Output: image/giftext/plain

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.

4. php uploads FILES to obtain the MIME type. If php uploads FILES and detects the MIME type of uploaded FILES, you can use the global variable $ _ FILES ['uploadfile'] ['type']. the client browser detects and obtains the file MIME type.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.