无法验证 php 中的文件类型

unable to validate file type in php

我正在使用PHPmailer进行文件附件验证,下面是代码供参考。根据要求,它应该只允许 PDF 或 DOCX 文件。但我在验证上传的文件时遇到问题。

if(is_array($_FILES) && $_FILES['attachmentFile']['type'] == "application/pdf" && $_FILES['attachmentFile']['type'] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" && $_FILES['attachmentFile']['size'] < 2097152)
{
$mail->AddAttachment($_FILES['attachmentFile']['tmp_name'],$_FILES['attachmentFile']['name']);
}
else
{
    echo "<p class='error'>Cannot upload other than PDF & DocX files > 2Mb.</p>";
    exit;
}

改变你的 if 条件:

if(is_array($_FILES) 
&& $_FILES['attachmentFile']['size'] < 2097152 
&& ($_FILES['attachmentFile']['type'] == "application/pdf" 
|| $_FILES['attachmentFile']['type'] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") )
{
$mail->AddAttachment($_FILES['attachmentFile']['tmp_name'],$_FILES['attachmentFile']['name']);
}
else
{
    echo "<p class='error'>Cannot upload other than PDF & DocX files > 2Mb.</p>";
    exit;
}