Php5.3 use fileinfo below to obtain the object mime type

Source: Internet
Author: User
Tags mime file
To obtain the mime type of the file below php5.3.0, the general method is to use the mime_content_type () method, but this method is outdated, so it is best to use fileinfo instead. I am using a windows environment. The following describes the windows environment. First download the php_fileinfo.dll file, you can

To obtain the mime type of the file below php5.3.0, the general method is to use the mime_content_type () method, but this method is outdated, so it is best to use fileinfo instead.

I am using a windows environment. The following describes the windows environment.

First download the php_fileinfo.dll file, you can download it here in the http://pecl.php.net/package/Fileinfo, but here the download is the source file, you need to compile. You can also download the http://dllcentral.com/php_fileinfo.dll/5.2.5.5/ here, but the bottom is 5.2.55. If the php environment you are using is xampp, you do not need to download it yourself. This is already integrated into xampp.

Download the file program from sourceforge.net (the file program is one of the programs in the GNUWin32 Project), the address is: http://sourceforge.net/projects/gnuwin32/files/file/, but it is best to choose Version 4.20 or earlier, because there is no magic in later versions. mime file. Download the binary program, which is used in file-x.xx-bin.zip. Decompress the downloaded file-x.xx-bin.zip file and copy the magic and magic. mime files that store the file type information from the share directory to a directory. Assume that the directory is D: \ xampp \ php \ extras. Here, magic can be replaced by magic in the conf directory of apache; the extras folder of PHP also has a magic. mime file, but the fileinfo extension cannot work properly (if you use the magic that comes with php. if mime is used, if fileinfo is used to obtain the object's mime type, it will be returned every time.) this magic cannot be used to replace GNUWin32. mime.

Modify php. ini and add the following line:

Extension = php_fileinfo.dll

Then restart the web server. Check whether Fileinfo is enabled correctly in phpinfo.

The test code is as follows:

// You can manually set the path of the magic File or configure it. The following is the configuration through php. ini.
// Obtain the absolute path of the magic File
$ MagicFile = get_cfg_var ('Magic _ mime. magic ')';
$ TestFile = 'd: \ test.doc ';
$ Finfo = new finfo (FILEINFO_MIME, $ magicFile );
Echo $ finfo-> file ($ testFile );
?>
Output: application/msword

Note: Make sure that the magic. mime and magic files are in the same folder.

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.