By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,491 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,491 IT Pros & Developers. It's quick & easy.

Uploading multiple files and processing them

P: 2
Hi all,

Just after some advice really. This is my first time posting on here, so please forgive me if my post isn't up to the required standard.

I have a form for emailing someone and it also allows the option to add file attachments. I have it working so that it will move a single file and email it and add the details to the database.

When I try with multiple files, it will only ever process the last file in the array.

This is what I have in the form

Expand|Select|Wrap|Line Numbers
  1. <input type="file" name="attachment[]" id="attachment" multiple/>
This is what I have in the PHP code for processing the file

Expand|Select|Wrap|Line Numbers
  1. if(is_array($fdata['name']))
  2. {
  3.     for($i = 0; $i < count($fdata['name']); ++$i)
  4.     {
  5.         $name_of_file = $_FILES['attachment']['name'][$i];
  6.         $file_name = $guid."-".$name_of_file;
  7.         $temp_name = $_FILES['attachment']['tmp_name'][$i];
  8.         $file_type = $_FILES['attachment']['type'][$i];
  9.         $file_size = $_FILES['attachment']['size'][$i];
  10.     }
  11. }
  12. else $files[] = $fdata;
  13.  
This will process only the last file.

If I put the following in the for loop, it will move the files as requested, but will not allow any other processing on it

Expand|Select|Wrap|Line Numbers
  1. $pics = array(".bmp", ".gif", ".jpg", "jpeg", ".png"); //5
  2. $docs = array(".doc", "docx", ".odt", ".pdf", ".ppt", "pptx", ".rtf", ".txt", ".xls", "xlsx"); //10
  3. $misc = array(".csv", ".htm", "html", ".php", ".pkt", ".rar", ".sql", ".xpi", ".zip"); //9
  4.  
  5. $base = basename($file_name);
  6. $extension = substr($base, strlen($base)-4, strlen($base));
  7. $extension = strtolower($extension);
  8.  
  9. if (in_array($extension,$pics))
  10. {
  11.     $target = "".FILES."/".FUP_PICS."/";
  12. }
  13.  
  14. if (in_array($extension,$docs))
  15. {
  16.     $target = "".FILES."/".FUP_DOCS."/";
  17. }
  18.  
  19. if (in_array($extension,$misc))
  20. {
  21.     $target = "".FILES."/".FUP_MISC."/";
  22. }
  23.  
  24. $target = $target.$base;
  25.  
  26. $allowed_extensions = array(".bmp", ".csv", ".doc", "docx", ".gif", ".htm", "html", ".jpg", ".JPG", "jpeg", "JPEG", ".odt", ".pdf", ".php", ".pkt", ".png", ".ppt", "pptx",    ".rtf", ".sql", ".txt", ".xls", "xlsx", ".zip");
  27.  
  28.  
This will then not allow me to access the variables outside the loop so I can further process it (email the file, add the details to the database0.

If I wrap it all in the for loop then it will only process the last file in the array.

I am somewhat stuck on the issue now.

I have tried a foreach loop, but it won't get the value of the variable in the file array to use for processing.

I will attach my file so you can see what I have there as it is around 800 lines long.

I'm looking for some pointers as to where I may be going wrong.

Thanks in advance
Attached Files
File Type: txt attach.txt (25.2 KB, 315 views)
Jul 21 '14 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.