Php methods for obtaining the object mime type

Source: Internet
Author: User

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:

Image/gif
Text/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 function by default.
Support-enabled), 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 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.

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.