添加一个文件类型验证到phpmailer? [英] Add a filetype validation to phpmailer?

查看:131
本文介绍了添加一个文件类型验证到phpmailer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个phpmailer,它通过从一个文件输入字段中遍历一个文件来连接文件。我怎样才能使PDF或DOC允许。如果还有别的事情发生,脚本会停止运行,并给出错误信息:文件类型不受支持,只有PDF或DOC。

有什么建议吗?我的当前脚本;
$ b $ pre $ foreach(array_keys($ _ FILES ['files'] ['name'])as $ key) {
$ source = $ _FILES ['files'] ['tmp_name'] [$ key];
$ filename = $ _FILES ['files'] ['name'] [$ key];
$ mail-> AddAttachment($ source,$ filename);


解决方案

您需要查看$ _FILES ['files'] ['type'],并确保它匹配你想要的MIME类型。

  foreach($ _FILES作为$ file){
if($ file ['files'] ['type'] =='application / msword'
|| $ file ['files'] ['type'] == 'application / pdf'
){
$ source = $ file ['files'] ['tmp_name'] [$ key];
$ filename = $ file ['files'] ['name'] [$ key];
$ mail-> AddAttachment($ source,$ filename);

else {
die(您只能通过此表单上传PDF和Microsoft Word文档。
}
}


I have a phpmailer that attaches files by looping through an array of files from an file input field. How can i make it so that onl PDF or DOC are allowed. And if something else is attempted, the script stope and gives an error: "File type not supported. Only PDF or DOC."

Any suggestions? Heres my current script;

foreach(array_keys($_FILES['files']['name']) as $key) {
        $source = $_FILES['files']['tmp_name'][$key];
        $filename = $_FILES['files']['name'][$key];
        $mail->AddAttachment($source, $filename);
}

解决方案

You need to look at $_FILES['files']['type'] and make sure it matches the mime types you want.

foreach ($_FILES as $file) {
    if ($file['files']['type'] == 'application/msword' 
        || $file['files']['type'] == 'application/pdf'
    ) { 
        $source = $file['files']['tmp_name'][$key];
        $filename = $file['files']['name'][$key];
        $mail->AddAttachment($source, $filename);
    }
    else {
        die("You may only upload PDF and Microsoft Word documents through this form.");
    }
}

这篇关于添加一个文件类型验证到phpmailer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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