【发布时间】:2014-07-18 12:12:09
【问题描述】:
我正在通过本地服务器上的 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;
}
【问题讨论】:
-
docx文件实际上是一个zip文件,只是扩展名不同。可能发生的情况是,在最近的 php 版本中添加了检查“zip”的内容
-
所以我必须升级php版本。
-
或者更改您的验证码,使其与两个版本兼容。
-
阅读“用户贡献的笔记”:php.net/manual/pt_BR/function.finfo-file.php
标签: php file file-upload mime-types