Search code examples
phpimap

Sending multiple attachments PHP imap only sending 1 attachment


script works but only sends one attachment. I can't seem to figure out what I'm doing wrong. It will send the first attachment $userfile['name'][0], but not $userfile['name'][1]

   $i = 0;
   foreach($_FILES as $userfile){
      $tmp_name = $userfile['tmp_name'][$i];
      $type = $userfile['type'][$i];
      $name = $userfile['name'][$i];
      $size = $userfile['size'][$i];
          $i++;
}

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
   enctype="multipart/form-data" name="form1">
 <input type="file" name="file[]" multiple />

original code is from here: How to attach two or multiple files and send mail in PHP


Solution

  • Change your filename to userfile[], then should be like this:

    Should be like this:

    $count_uploaded_files = count( $_FILES['userfile']['name'] );
    
    for( $i = 0; $i < $count_uploaded_files; $i++ )
    {
        $tmp_name = $_FILES['userfile']['tmp_name'][$i];
        $type     = $_FILES['userfile']['type'][$i];
        $name     = $_FILES['userfile']['name'][$i];
        $size     = $_FILES['userfile']['size'][$i];
    
        // Do whatever you want with $temp_name, $type, $name, and $size ...
    }
    

    But if you want to stay with file[]. then like this:

    $count_uploaded_files = count( $_FILES['file']['name'] );
    
    for( $i = 0; $i < $count_uploaded_files; $i++ )
    {
        $tmp_name = $_FILES['file']['tmp_name'][$i];
        $type     = $_FILES['file']['type'][$i];
        $name     = $_FILES['file']['name'][$i];
        $size     = $_FILES['file']['size'][$i];
    
        // Do whatever you want with $temp_name, $type, $name, and $size ...
    }