【问题标题】:File attachment type文件附件类型
【发布时间】:2012-06-07 18:42:22
【问题描述】:

您好,我有发送带有多个附件的电子邮件的代码,但我对下面的一段代码有疑问。

每当我提交图片时,我都会收到“不支持的文件格式!”。如果我删除那部分代码,它会很好用,但是用户可以发送每种类型的文件(我知道这很危险)。文件大小检查效果很好。怎么了? :S

//*** Attachment ***//  
for($i=0;$i<count($_FILES["fileAttach"]["name"]);$i++)  
{
    $filesize = $_FILES["fileAttach"]["size"][$i]/1024;

    if($_FILES["fileAttach"]["name"][$i] != "" && $filesize < 50 && preg_match("image/", $_FILES["fileAttach"]["type"][$i]))  
    {  
    $strFilesName = $_FILES["fileAttach"]["name"][$i];  
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"][$i])));  
    $strHeader .= "--".$strSid."\n";  
    $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
    $strHeader .= "Content-Transfer-Encoding: base64\n";  
    $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";  
    $strHeader .= $strContent."\n\n";  
    }
    elseif(!preg_match("image/", $_FILES["fileAttach"]["type"][$i])){
        die('<div id="warning">Unsuported file format!</div>');
    }
    elseif($filesize > 50){
        die('<div id="warning">Filesize must be less than 2 Mb!!!</div>');
        }
}

【问题讨论】:

    标签: php email sendmail attachment


    【解决方案1】:
    $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
    

    应该是

    $strHeader .= "Content-type: application/octet-stream\n";
    

    没有文件名部分。这进入内容处置行。

    在大局观中,不要建立自己的 mime 电子邮件。正如你所发现的那样,它既痛苦又丑陋。使用 PHPMailerSwiftmailer 为您完成 - 整个附件业务可以简化为使用任一库的 SINGLE 函数调用。

    【讨论】:

    • 好吧,tnx,我将在未来的项目中使用它。但我不能大量修改现有项目(在许多网站上),所以我试图以某种方式解决这个“错误”,但我就是想不通。
    猜你喜欢
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    相关资源
    最近更新 更多