简体   繁体   中英

Third file is attached only, through phpmailer

I m sending an Email with multiple attachments through phpmailer. The loop i used to send multiple attachments is The Message going with attachment, only contain the last choosen file, and the first two or one files are not sent. Is there a problem with Loop. Please :

     foreach($_FILES as $key => $file){
    $target_path = "uploads/";
    $target_path = $target_path .basename($file['name']);

   if(move_uploaded_file($file['tmp_name'], $target_path)) {
   echo "the file ".basename($file['name'])." has been uploaded";
   }else {
  echo "there was an error";
   }
  mail->AddAttachment($target_path);
   }

     <form id=
       "mail" name="mail" method="POST" action="<?php $PHP_SELF ?>" 
    enctype="multipart/form-data">
     <input type="file" name="uploaded" multiple="" />
    <input name="Submit1" type="submit" value="Submit"  />
    </form>

Add [] to the name of the input field, like so:

<input type="file" name="uploaded[]" multiple="" />

Each file uses the same name "uploaded", so "uploaded" gets replaced by each successive file that is processed. By adding brackets, each successive file is added to the "uploaded" array instead. Here's the $_FILES array I get after adding [] :

Array
(
  [uploaded] => Array
    (
      [name] => Array
        (
          [0] => file4.txt
          [1] => file1.txt
          [2] => file2.txt
          [3] => file3.txt
        )

      [type] => Array
        (
          [0] => text/plain
          [1] => text/plain
          [2] => text/plain
          [3] => text/plain
        )

      [tmp_name] => Array
        (
          [0] => C:\temp\php95.tmp
          [1] => C:\temp\php96.tmp
          [2] => C:\temp\php97.tmp
          [3] => C:\temp\php98.tmp
        )

      [error] => Array
        (
          [0] => 0
          [1] => 0
          [2] => 0
          [3] => 0
        )

      [size] => Array
        (
          [0] => 7
          [1] => 2850
          [2] => 27
          [3] => 231
        )
    )
)

Index 0 in each array is one file, index 1 in each array is the next file, and so on.


This is also how you would get the values of multiple checkboxes of the same name. (See this comment for an example.


Here's how you could loop through the uploaded files.

// first get the count of how many files are uploaded
$numFiles = count(array_filter($_FILES['uploaded']['name']));

for ($i = 0; $i < $numFiles; ++$i) {
    $target_path = 'c:/temp/' . basename($_FILES['uploaded']['name'][$i]);
    if(move_uploaded_file($_FILES['uploaded']['tmp_name'][$i], $target_path)) {
        echo "the file ".basename($_FILES['uploaded']['name'][$i])." has been uploaded<br />";
    }
}

Notice how I use $i in a for loop to keep track of the index of the current file.

(If you're wondering why I called array_filter() to get the count, it looks like a blank entry automatically exists if you don't upload any files. array_filter() removes that blank/invalid entry.)

Try adding a unique number to each file on each iteration through the loop like this:

$counter = 0;
foreach($_FILES as $key => $file){
    $counter++;
    $target_path = "uploads/";
    $target_path = $target_path .basename($file['name'], ".jpg") . $counter . ".jpg";



if(move_uploaded_file($file['tmp_name'], $target_path)) {
   echo "the file ".basename($file['name'])." has been uploaded";
   }else {
  echo "there was an error";
   }
  mail->AddAttachment($target_path);
   }

 <form id=
   "mail" name="mail" method="POST" action="<?php $PHP_SELF ?>" 
enctype="multipart/form-data">
 <input type="file" name="uploaded" multiple="" />
<input name="Submit1" type="submit" value="Submit"  />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM