Home  >  Article  >  Backend Development  >  The finfo_file function obtains the solution to the file mime value verification error_PHP tutorial

The finfo_file function obtains the solution to the file mime value verification error_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:29:141143browse

When I was uploading images today and verifying the mime value of the image, I suddenly discovered that in some special cases, the MIME value obtained by finfo_file cannot be used directly.

According to the official writing, it is

$finfo=finfo_open(FILEINFO_MIME);
$mime=finfo_file($finfo,$file_path);
finfo_close($finfo);
alert($mime);

Get the mime type of the file like this

But today I found that this doesn’t work. If there is a charset setting the transfer type to binary stream during file transfer, something similar to the picture below will appear:

You can clearly see that there are more semicolons and the following things charset=binary

If the file mime value is verified here, even if it is a correct and legal file type, it will not pass the verification, because the obtained mime value is followed by a part of the binary file stream string "; charset=binary"

$file_name = $_FILES['imgFile']['name'];

$temp_arr = explode(".", $file_name);
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);

$_mime=array('jpg'=>array('image/pjpeg','image/jpeg'),'gif'=>array('image/gif'),'png'=> array('image/x-png','image/png'),'jpeg'=>array('image/jpeg','image/pjpeg'));

if(empty($mime) || !in_array($mime,$_mime[$file_ext])){
alert('Picture mime type error!');
}

Therefore, compatibility processing under special environmental requirements needs to be done

The modified general method of obtaining the compatibility of mime types is as follows (pay attention to the red parts, and obtain the correct mime value through regular expressions that are compatible with multiple requirements environments):

if(empty($mime) && function_exists('finfo_open')){
$finfo=finfo_open(FILEINFO_MIME);
$mime=finfo_file($finfo,$file_path);
finfo_close($finfo);
//Compatible with precise mime verification of file upload in special applications
$new=preg_match('/([^;]+);?.*$/',$mime,$match);
if($new) $mime=trim($match[1]);
alert($mime);
}

In this way, you can correctly obtain the mime type in the compatible environment and perform correct file mime legality verification. The running results are as shown in the figure:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/777566.htmlTechArticleToday when I was uploading an image to verify the mime value of the image, I suddenly discovered that in some special cases, the MIME value obtained by finfo_file cannot be used directly. , according to the official writing method is $finfo=finfo_open(FILEINF...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn