我有一个表单,我正在使用PHPMailer将该表单中的数据发送到我的电子邮件.用户也可以发送附件,但我有一个问题:如何让PHPMailer拒绝大于2Mb的附件,并且只允许iamge附件(没有其他类型的文件)?
这是我用于PHPMailer的多个电子邮件附件的代码:
foreach(array_keys($_FILES['fileAttach']['name']) as $key) {
$source = $_FILES['fileAttach']['tmp_name'][$key];
$filename = $_FILES['fileAttach']['name'][$key];
$mail->AddAttachment($source, $filename);
}
Run Code Online (Sandbox Code Playgroud)
您可以使用filesize()和检查文件大小和类型mime_content_type().
生成的代码可能如下所示:
$maxsize = 2 * 1024 * 1024; // 2 MB
$types = array('image/png', 'image/jpeg', 'image/gif'); // allowed mime-types
if(filesize($filename) < $maxsize && in_array(mime_content_type($filename),$types)){
$mail->AddAttachment($source, $filename);
}
Run Code Online (Sandbox Code Playgroud)
编辑: PHPMailer没有这些chacks的内置可能性 - 正如您从源代码中看到的,它只检查添加附件时文件是否存在:
if ( !@is_file($path) ) {
throw new phpmailerException($this->Lang('file_access') . $path, self::STOP_CONTINUE);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6421 次 |
| 最近记录: |