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.