Several ways to get data MIME types in PHP

Source: Internet
Author: User
Several ways to get file MIME types in PHP

? A MIME type is a type of file that sets an extension that is opened by an application, and the browser automatically opens with the specified application when the extension file is accessed. Many are used to specify some client-customized file names, as well as some ways to open media files.

PHP Gets the file MIME type in several ways:

1, Mime_content_type () function to determine the MIME type obtained

? Mime_content_type returns the MIME type of the specified file,

usage:
echo Mime_content_type ( ' Php.gif ' Span style= "color: #007700;" >) . echo mime_content_type ( ' test.php ' );
output:

Image/gif
Text/plain

But PHP 5.3.0 has discarded the function. If you still want to use this function, you can configure PHP to enable the magic_mime extension.

2. PHP Fileinfo Get file MIME type (finfo_open)

? PHP official recommendation The alternative function of Mime_content_type () is the FileInfo function. PHP 5.3.0+ already supports the FileInfo function (FileInfo support-enabled) By default, and you can use Finfo_open () to determine the file MIME type without having to make any configuration.
Usage:
$finfo??? = Finfo_open (fileinfo_mime);
$mimetype = Finfo_file ($finfo, $filename);
Finfo_close ($finfo);

3, Image_type_to_mime_type () Get the picture MIME type

? If you need to determine the MIME type of files only image files, then you can first use the Exif_imagetype () function to get the image type constants, and then use the Image_type_to_mime_type () function to convert the image type constants to the MIME type of the picture file.
Note: You need to configure open Php_mbstring.dll (Windows required) and Extension=php_exif.dll in php.ini.

Image_type_to_mime_type () Get picture MIME type

If we need to determine that MIME-type files are only image files, you can first use the Exif_imagetype () function to get the image type constants, and then use the Image_type_to_mime_type () function to convert the image type constants to the MIME type of the picture file. The same php.ini to be configured to open Php_mbstring.dll (Windows requires) and Extension=php_exif.dll. Phpinfo () "–enable-exif". First Exif_imagetype returns the image type constant (Imagetype Constants), such as imagetype_gif,imagetype_jpeg,imagetype_ PNG and so on.

 
  

4. PHP upload file to get MIME type

If you upload a file using PHP to detect the MIME type of the uploaded file, you can use the global variable $_files[' uploadfile ' [' type '], which is detected by the client's browser to get 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.