Now that I have some time to not work on something for someone or another I figure I’d post this.
Uploading multiple files at once used to be strictly in the realm of flash or required javascript to crate multiple upload forms as needed. The first method has the problems with the general incompatibility of flash. The second, well, it’s a very low tech method and limits you to selecting only one file per upload form, real pain.
It’s a two step problem, step one, the form:
Pretty standard form, all you gotta do is add the multiple="multiple" in there and add brakets ([]). Those brakets are the key to the next set, they tell PHP to make the file variable an array so it stacks the files up, instead of just replace the it with the next one (laving you with only the last file uploaded).
Next thing to do is tell PHP up handle the uploaded files one by one. This is pretty simple, heres a quick one.
|
1 |
for($i=0;$i |
Snipplr: http://fatfolderdesign.com/ex/multi-upload/index.php
First thing you have to do is get the number of files, I used the count() of the file size because every file has a size, but the same can be said for any $_FILES attribute, so they all work equally well. Using a similar method it’s easy to retrofit an old upload system (which was my original implementation) simple by adding it in the loop and adding the variable to the end of the $_FILES call and it all just works.
This information was surprising difficult to find (most searches returned flash based solutions) but implementation on the existing system (that used FTP to transfer uploaded files to another server and wrote information in multiple databases) was far simpler than any of the flashed based ones, and it’s small and compact which is an added bonus.
An example of the above code can be found here: http://fatfolderdesign.com/ex/multi-upload/index.php.
Edit 2/12/11 Changed the display for the “view uploads” page.
Edit 1/22/13 As requested I’ve made the source of the uploads page viewable here: http://fatfolderdesign.com/ex/multi-upload/upload.php.txt