How to get the file MIME type in PHP by way of explanation

Source: Internet
Author: User
Tags mime file
how PHP Gets the file MIME type

1. Using the Mime_content_type method

String Mime_content_type (String $filename) Returns the MIME content type for a file as determined by using information f Rom the magic.mime file.
<?php$mime_type = Mime_content_type (' 1.jpg '); Echo $mime _type; Image/jpeg?>

However, this method was abandoned above php5.3 , and the official proposed to use the FileInfo method instead.

2. Using the Fileinfo method (official recommendation)

The php_fileinfo extension needs to be installed using FileInfo.

If installed can be found in the Extension_dir directory php_fileinfo.dll(Windows),fileinfo.so(Linux)

Open php.ini, put Extension=php_fileinfo.dll before the ";" Remove, and then restart Apache.

<?php$fi = new Finfo (fileinfo_mime_type), $mime _type = $fi->file (' 1.jpg '); Echo $mime _type; Image/jpeg?>

3. Using the Image_type_to_mime_type method (only image types can be processed)

Use the Exif_imagetype method to install the php_exif extension and need to install the php_mbstring extension

If installed can be found in the Extension_dir directory php_exif.dll(Windows),exif.so(Linux)

Open the php.ini, put Extension=php_mbstring.dll, Extension=php_exif.dll before the "," remove, and then restart Apache

<?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 filename to judge, because the file suffix can be modified, so use the file suffix to determine the inaccuracy.

This article explains how to get the file MIME type through PHP, and more about the PHP Chinese web.

Related recommendations:

How to get all YouTube user video information via PHP

PHP character encoding conversion class related content

Explanation of the performance of PHP Implode/explode, serialize, JSON, Msgpack

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.