finfo_file function Get file MIME value validation error resolution _php Tutorial

Source: Internet
Author: User
Today in the image upload to verify the MIME value of the image suddenly found that the individual special circumstances Finfo_file get MIME value can not be used directly,

According to the official wording,

$finfo =finfo_open (fileinfo_mime);
$mime =finfo_file ($finfo, $file _path);
Finfo_close ($finfo);
alert ($mime);

This gets the file MIME type of the

But it's not working out today. If you have CharSet set the transport type as a binary stream in a file transfer, it will appear similar to the following:

It can be seen clearly that there are more semicolons and back things behind here charset=binary

If the file MIME value is verified again, even the correct and valid file type cannot be verified, because the MIME value obtained is followed by a portion 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 need to do under special environmental requirements of the compatibility treatment

The following general method of obtaining the compatibility of the modified MIME type is as follows (note the red portion of the column, which is obtained correctly by getting the MIME value in the correct compatible multi-demand environment):

if (Empty ($mime) && function_exists (' Finfo_open ')) {
$finfo =finfo_open (fileinfo_mime);
$mime =finfo_file ($finfo, $file _path);
//compatible special applications File upload MIME precision verification
$new =preg_match ('/([^;] +);?. *$/', $mime, $match);
if ($new) $mime =trim ($match [1]);
alert ($mime);

In this way, you can correctly obtain the MIME type of the compatible environment to verify the validity of the file mime, and run the result:

http://www.bkjia.com/PHPjc/777566.html www.bkjia.com true http://www.bkjia.com/PHPjc/777566.html techarticle today in the image upload to verify the MIME value of the image is suddenly found in individual special cases Finfo_file obtained MIME value can not be used directly, according to the official wording is $finfo =finfo_open (Fileinf ...

  • 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.