0

我有一个简单的页面,它通过 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 一起工作。请任何身体帮助

4

1 回答 1

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);
    }
}
于 2012-04-17T11:36:27.550 回答