无法在php中验证文件类型

时间:2015-08-11 13:25:29

标签: php file validation phpmailer

使用php邮件程序无法正常工作的文件附件验证。

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

1 个答案:

答案 0 :(得分:0)

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