将附件移动到文件夹并使用电子邮件附加它们

时间:2012-04-16 19:35:02

标签: php upload attachment email-attachments

我有一个简单的页面,它通过phpmailer发送电子邮件和多个附件。 我必须将多个附件附加到要发送的电子邮件消息,并同时将这些文件上传到服务器,为此我使用以下循环:

 $MyUploads = array();

 foreach(array_keys($_FILES['attach']['name']) as $key)
 { $Location="uploads/";

 $name=$_FILES['attach']['name'][$key];
 $filePath = $Location . $name;
 $source = $_FILES['attach']['tmp_name'][$key]; // location of PHP's temporary file for 
 $tmp=$_FILES['attach']['tmp_name'][$key];

 if($mail->AddAttachment($source, $name))
  {if(move_uploaded_file($tmp, $filePath)){
 $MyUploads[] = $filePath;}
 else
 {$MyUploads[]='';
 echo "not uploaded";}
 }
 }

问题是,当我使用函数move_uploaded_file()时,文件会上传到服务器文件夹,但不会随附件一起发送。当我评论这个功能时,会附上附件。

可以找出,为什么这两个dnt一起工作。请任何身体帮助

1 个答案:

答案 0 :(得分:0)

这是发送附件的循环,并将它们移动到目标路径,以供进一步使用。我希望其他任何人都能得到这方面的帮助:

$MyUploads = array();
$numFiles = count(array_filter($_FILES['attach']['name']));

for ($i = 0; $i < $numFiles; ++$i) {
    $target_path = 'uploads/' . basename($_FILES['attach']['name'][$i]);
    if(move_uploaded_file($_FILES['attach']['tmp_name'][$i], $target_path)) {
        echo "the file ".basename($_FILES['attach']['name'][$i])." has been uploaded<br />";
        $MyUploads[] = $target_path;
        echo $MyUploads;
        $mail->AddAttachment($target_path);
    }
}