Fileinfo returns different mimetype on php version 5.4.22 and 5.3.3

I am writing custom file validation through mimetype on my local server php version is 5.4.22 and it returns "docx" file mimetype "application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=binary" that is correct for me.

But on my server there is php version 5.3.3 and it returns "docx" file mimetype "application/zip; charset=binary" that is not correct and my validation fails here.

Please suggest what i have to do, i should upgrade the php version on server 5.3.3 to php latest version.

function hook_file_validate($file) {
    $errors = array();
    //Getting filename
    $extn = explode(".", $file->filename);
    //Getting file mimetype
    $finfo = new finfo(FILEINFO_MIME);
    $type = $finfo->file($file->uri);

    if ($extn[1]=='txt' && $type!='text/plain; charset=us-ascii'){
        $errors[] = t("Please upload valid file");
    } else
    if ($extn[1]=='doc' && $type!='application/msword; charset=binary'){
        $errors[] = t("Please upload valid file.");
    } else
    if ($extn[1]=='pdf' && $type!='application/pdf; charset=binary'){
        $errors[] = t("Please upload valid file.");
    } else
    if ($extn[1]=='xls' && $type!='application/octet-stream; charset=binary'){
        $errors[] = t("Please upload valid file.");
    } else
    if ($extn[1]=='docx' && $type!='application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=binary')    {
        $errors[] = t("Please upload valid file.");
    }

    return $errors;
}

Check the file extension after you get the application/zip mime type. Here is the code

$arrayZips = array("application/zip", "application/x-zip", "application/x-zip-compressed");
$arrayExtensions = array(".pptx", ".docx", ".dotx", ".xlsx");
$file = 'path/to/file.xlsx';
$original_extension = (false === $pos = strrpos($file, '.')) ? '' : substr($file, $pos);
$finfo = new finfo(FILEINFO_MIME);
$type = $finfo->file($file);
if(in_array($type, $arrayZips) && in_array($original_extension, $arrayExtensions)){
    return $original_extension;
}
链接地址: http://www.djcxy.com/p/45482.html

上一篇: 使用Tika进行MIME类型检测会带来意想不到的结果

下一篇: Fileinfo在php版本5.4.22和5.3.3上返回不同的mimetype