PHP get file MIME type FileInfo etc methods

Source: Internet
Author: User
Tags deprecated mime file php switch phpinfo

A few days ago wrote to use the WordPress XMLRPC API Remote Publishing articles, if the local server article library in a post to be published in a WordPress article contains image files, will use the WordPress upload file API Metaweblog.newmediaobject, the API needs to provide the MIME type of the file. How does PHP get the MIME type of a file (picture)? The original remote blog uses the PHP mime_content_type () function, which is fully supported after testing with the default configuration of Ubuntu server lamp for development, returning the correct file MIME type. However, when porting the API project to the CentOS 5.2 (kernel 2.6) lamp environment, the following error message appears:

Fatal error:call to undefined Function:mime_content_type ()

Finally, the latest PHP manual found that PHP mime_content_type () function has been deprecated, of course, the official is not recommended, and need to pass the appropriate PHP configuration to use. So to get the mime type of a picture or other file, the Fatal error:call to undefined Function:mime_content_type () error has the following solutions.

Mime_content_type () function to determine which MIME type to get

If you still love the Mime_content_type () function that has been discarded by PHP 5.3.0, you can configure PHP to enable Magic_mime extensions. For example, under CentOS use Phpinfo () view PHP Apache configuration, find to Mime-magic, if the "--without-mime-magic", then to compile PHP switch to "with-mime-magic" option. The Mime_content_type () function also relies on the Apache httpd Magic File (Mime_magic.magicfile), in order to detect the MIME type of the file, you must configure the address that tells the magic file, such as ' –with- Mime-magic=/usr/share/file/magic.mime '. The Windows environment also needs to be added in php.ini:
Mime_magic.magicfile = "$PHP _install_dirmagic.mime"

Where $php_install_dir is your PHP installation directory. In some lamp environments, this mime_magic file may not exist or be readable, and it must be downloaded separately. In addition, some virtual hosts do not necessarily return the correct MIME type for security reasons, sometimes returning an empty string, even if there are with-mime-magic. Therefore, it is not recommended to use this method to get the file MIME type, since the Mime_content_type () function has been deprecated.

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. Centos default installation of the lamp environment PHP version or PHP5.2.6, less than 5.3.0 version may appear similar error prompt:php Fatal error:call to undefined function finfo_open () in< /c2> .... Because of the previous PHP version, the Magic_open class needs to be loaded, the FileInfo function belongs to the pecl extension, and the FileInfo pecl extension is enabled to detect MIME types. So there are two ways to get the MIME type of a file using FileInfo.

  • Upgrade the PHP version to 5.3.0 or more. PHP has also no longer maintained and updated the FileInfo pecl expansion pack, so upgrading is the best approach.
  • Installing the FileInfo pecl extension,How to install FileInfo on Centos Linux: Install the FileInfo Command (RPM) under CentOS:Yum Install Php-pecl-fileinfo。 or compile with source code installation:




    Cd/Usr/Src/Down&&wget http://pecl.php.net/get/fileinfo-1.0.4.tgz tar zxvf  fileinfo-1.0. 4.tgz&NBSP;CD /usr//down/fileinfo-< Span class= "lit" >1.0. 4 && phpize &&  ./configure &&  make && make install       
    • if no phpize instructions are required, install it first. #phpize检测若提示 "No command ' phpize ' found", you need to install Phpize first,
    • download the rpm installation Php-devel (Php5-dev), install phpize;
    • Service httpd restart or reboot;
    • command pecl Install FileInfo installation FileInfo extension.
    • installed, the/usr/lib/php/module directory has more than fileinfo.so files,/usr/share/file directory Magic.mime and Magic two documents
    • Modify php.ini configuration: Join extension= "fileinfo.so"
    • service httpd restart
    • Windows Server installation FileInfo similar, Php.ini:extension=php_fileinfo.dll






Span style= "FONT-SIZE:14PX;" >image_type_to_mime_type () Gets the picture MIME type

If we need to determine that MIME-type files are only image files, you can first use the exif_ The ImageType () function gets the image type constants, and then uses 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 constants (Imagetype Constants), such as Imagetype_gif, Imagetype_jpeg, Imagetype_png, and so on.

<?= Exif_imagetype ( "D: Farleeinfo.jpg ");    //local path or remote picture address can  imagetype_gif//  $image Span class= "pun" >= Exif_imagetype ();   $mime = Image_type_to_mime_type ( Span class= "PLN" > $image );  echo $mime ;  //output image/jpeg< Span class= "PLN" > ?>           



PHP upload file 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.

In the Centos system or other environment if it is not convenient to get file MIME type, there is the last absolute feasible method, is to read the file name suffix, according to the suffix name one by one corresponding file MIME type, specifically can refer to the PHP Manual of this comment. Of course, the MIME file type detected by this method is not necessarily very accurate.

PHP get file MIME type FileInfo etc methods

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.