Add a filetype validation to phpmailer?

回眸只為那壹抹淺笑 提交于 2019-12-24 02:39:07

问题


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

回答1:


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.");
    }
}


来源:https://stackoverflow.com/questions/6985994/add-a-filetype-validation-to-phpmailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!