Fileinfo在php 5.4.22和5.3.3版本返回不同的mime类型



我在本地服务器上通过mimetype编写自定义文件验证php版本是5.4.22,它返回"docx"文件mimetype "application/vnd.openxmlformats-officedocument.wordprocessingml.document;Charset =binary"这对我来说是正确的

但是在我的服务器上有php版本5.3.3,它返回"docx"文件mimetype "application/zip;Charset =binary"这是不正确的,我的验证在这里失败。

请建议我该怎么做,我应该把服务器5.3.3的php版本升级到php最新版本。

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;
}

在获得application/zip mime类型后检查文件扩展名。下面是代码

$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;
}

相关内容

最新更新