Php: how to obtain the object mime type

Source: Internet
Author: User
Tags mime file
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.

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.