1

If I use multiple file fields, those can be retrieved by using foreach. This method is working fine.

<input type="file" name="attachFile">
<input type="file" name="attachFile2">
<input type="file" name="attachFile3">
foreach($_FILES as $attachFile)
{
  $tmp_name = $attachFile['tmp_name'];
  $type = $attachFile['type'];
  $name = $attachFile['name'];
  $size = $attachFile['size'];
}

If I do the same for HTML5 multiple file field that does not work. Here is the example below that I have no luck with. It does not produce any error either.

<input multiple="multiple" type="file" name="attachFile">
foreach($_FILES['attachFile'] as $attachFile)
{
  $tmp_name = $attachFile['tmp_name'];
  $type = $attachFile['type'];
  $name = $attachFile['name'];
  $size = $attachFile['size'];
}
3
  • I'm assuming this is PHP on the server side? Commented Mar 16, 2013 at 6:11
  • The name of your input should be attachFile[] instead of attachFile - See this answer: stackoverflow.com/a/8725752/921204 Commented Mar 16, 2013 at 6:13
  • Yes that's PHP. I was trying to send email with multiple attachments by using single input field. Commented Mar 16, 2013 at 10:35

1 Answer 1

1

Do it like this. Create a function and first figure out how many files are being attached. Use that function to attach each file.

function reArrayFiles(&$attachFile) 
  {
        $file_ary = array();
        $file_count = count($attachFile['name']);
        $file_keys = array_keys($attachFile);
        for ($i=0; $i<$file_count; $i++) 
       {
            foreach ($file_keys as $key) 
            {
                $file_ary[$i][$key] = $attachFile[$key][$i];
            }
        }
        return $file_ary;
    }


          $file_ary = reArrayFiles($_FILES['attachFile']); 
          foreach($file_ary as $file)
          {
             $tmp_name = $file['tmp_name'];
             $type = $file['type'];
             $name = $file['name'];
             $size = $file['size'];
          }

And use input for multiple files properly.

<input multiple="multiple" type="file" name="attachFile[]">
Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.