Adding Multiple Attachments to PHPMailer

I am trying to attach multiple images in attachments. I used forearch for every attachment, but without getting the name and the name temp, when I use foreach, I probably am doing something wrong. Below is the code and errors:

HTML input

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

var_dump of the file $ _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) } } 

var_dump for name and temp_name:

 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 

CODE:

 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); } 
+6
source share
4 answers

Welcome to the evil side of PHP. $_FILES not what the developer expected.

 //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]; 

So you need something like

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

Thanks for all the answers. I am sure that all your approaches will work fine, but I decided to solve it myself. This code solved the problem

 $validAttachments = array(); foreach($_FILES['upload-file']['name'] as $index => $fileName) { $filePath = $_FILES['upload-file']['tmp_name'][$index]; $validAttachments[] = array($filePath, $fileName); } foreach($validAttachments as $attachment) { $mail->AddAttachment($attachment[0], $attachment[1]); } 

I hope that anyone who has the same problem will get some help from here ...

+2
source
 $i = '0'; foreach($_FILES['upload-file'] as $file) { $name = $file['name'][$i]; $path = $file['tmp_name'][$i]; var_dump($name); var_dump($path); $mail->addattachment($path,$name); $i++; } 
0
source

Most of the solutions here are based on forms.

So, I came up with a simple solution if you want to attach all the files from a specific directory.

 $file_to_attach_directory = 'files/'; if ($handle = opendir($file_to_attach_directory)) { try { while (false !== ($entry = readdir($handle))) { $attachment_location = $file_to_attach_directory. $entry; $mail->addAttachment($attachment_location); } closedir($handle); // Send Mail if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } } catch (Exception $e) { var_dump($e); } } 
0
source

Source: https://habr.com/ru/post/983745/


All Articles