将附件移动到文件夹并通过电子邮件附加它们



我有一个简单的页面,它通过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() 时,文件会上传到服务器文件夹,但不与附件一起发送。当我注释掉此功能时,将发送附件。

不知道,为什么这两者不能一起工作。请任何身体帮助

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

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

相关内容

  • 没有找到相关文章

最新更新