PHPMailer附件类型和大小限制 [英] PHPMailer attachment type and size limit

查看:403
本文介绍了PHPMailer附件类型和大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,我正在使用PHPMailer将数据从该表单发送到我的电子邮件中.用户也可以发送附件,但是我有一个问题:如何使PHPMailer拒绝大于2Mb的附件并仅允许iamge附件(不允许其他类型的文档)?

i have one form and i am using PHPMailer to send data from that form to my email. Users can send attachments as well, but i have one rpoblem: how to make PHPMailer to deny attachments larger than 2Mb and to allow only iamge attachments (no other types of documents)?

这是我用于通过PHPMailer倍增电子邮件附件的代码:

This is code i using for multiply email attachments with PHPMailer:

foreach(array_keys($_FILES['fileAttach']['name']) as $key) {

   $source = $_FILES['fileAttach']['tmp_name'][$key]; 
   $filename = $_FILES['fileAttach']['name'][$key]; 

   $mail->AddAttachment($source, $filename);

}

推荐答案

,您可以使用 filesize() 和使用 .

结果代码如下:

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

编辑:PHPMailer并没有内置这些​​小工具的功能-从源代码中可以看到,它仅在添加附件时检查文件是否存在:

PHPMailer doesn't have a built-in possibility for those chacks - as you can see from the source, it only checks if the file exists when adding an attachment:

if ( !@is_file($path) ) {
  throw new phpmailerException($this->Lang('file_access') . $path, self::STOP_CONTINUE);
}

这篇关于PHPMailer附件类型和大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆