在PHPMailer中添加多个附件

Fah*_*ail 6 php attachment mailer email-attachments

我试图在附件中附加多个图像.我使用forearch作为每个附件但是,当我使用foreach时它没有得到临时名称和名字,我可能做错了.以下是代码和错误:

输入HTML

<input id="upload-file" class="upload-file" type="file" name="upload-file[]">

var_dump $ _FILES ['upload-file']:

array(5) { ["name"]=> array(1) { [0]=> string(47) "WRANGLER_AW13_GIRLONTOP_A4_LANDSCAPE_300dpi.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(24) "C:\xampp\tmp\php41DC.tmp" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(91742) } } 
Run Code Online (Sandbox Code Playgroud)

名称和temp_name的var_dump:

Notice: Undefined index: name in C:\xampp\htdocs\hmg\process-email.php on line 66

Notice: Undefined index: tmp_name in C:\xampp\htdocs\hmg\process-email.php on line 67

NULL 
NULL
Run Code Online (Sandbox Code Playgroud)

码:

foreach($_FILES['upload-file'] as $file) {         

    $name = $file['name'];
    $path = $file['tmp_name'];
    var_dump($name);
    var_dump($path);

    //And attach it using attachment method of PHPmailer.

    $mail->addattachment($path,$name);
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*rdt 7

欢迎来到PHP的邪恶方面.该$_FILES不是,开发人员可以预料到的.

//wrong code
$img1 = $_FILES['upload-file'][0]['tmp_name'];
$img2 = $_FILES['upload-file'][1]['tmp_name'];

//working code
$img1 = $_FILES['upload-file']['tmp_name'][0];
$img2 = $_FILES['upload-file']['tmp_name'][1];
Run Code Online (Sandbox Code Playgroud)

所以你需要类似的东西

$totalFiles = count($_FILES['upload-file']['tmp_name']);
for ($i = 0; $i < $totalFiles; $i++) {
   $name = $_FILES['upload-file']['name'][$i];
   $path = $_FILES['upload-file']['tmp_name'][$i];
   $mail->addattachment($path,$name);
}
Run Code Online (Sandbox Code Playgroud)

以下是PHPMailer存储库中的一些示例.