在这种情况下,您可以通过以下脚本获取扩展名来尝试在 mime 类型和扩展名之间进行交叉验证:
$fileExtension= end(explode(".", $_FILES["uploadedFile"]["name"]));
或
$fileName = ($_FILES['uploadedFile']['name']);
$fileExtension = pathinfo($fileName , PATHINFO_EXTENSION);
然后,应用类似的东西:
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $fileExtension = array_search(
$finfo->file($_FILES['uploadedFile']['tmp_name']),
array(
//'sh' => 'text/x-shellscript', //not allowed
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'rtf' => 'text/rtf',
'odt' => 'application/vnd.oasis.opendocument.text',
'txt' => 'text/plain',
'pdf' => 'application/pdf',
),
true
)) {
$error .= "<br> The allowed file format file are: \"doc\", \"docx\", \"rtf\", \"odt\", \"txt\", \"pdf\"' ";
}
我遇到了类似的问题,但在我的情况下,文件是 .rtf 类型。
FILEINFO_MIME_TYPE 函数显然无法捕获任何类型的文件扩展名,这可能会导致一些验证错误。
一些例子:
.rtf 文件的默认 mime 类型是 application/rtf,但 FILEINFO_MIME_TYPE 函数显示 text/rtf。
正如我在here 描述的那样,我浪费了很多时间来解决这个错误:
对于.sh 文件,我注意到FILEINFO_MIME_TYPE 函数无法捕获扩展名,它返回一个空值