Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Multiple File Upload Input

23 Responses »

Multiple File Upload

More often than not, I find myself wanting to upload more than one file at a time.  Having to use multiple "file" INPUT elements is annoying, slow, and inefficient.  And if I hate them, I can't imagine how annoyed my users would be.  Luckily Safari, Chrome, and Firefox have implemented a method by which users can upload multiple files within one INPUT element.

The HTML

<!-- IMPORTANT:  FORM's enctype must be "multipart/form-data" -->
<form method="post" action="upload-page.php" enctype="multipart/form-data">
  <input name="filesToUpload[]" id="filesToUpload" type="file" multiple="" />
</form>

Simply adding the multiple attribute allows for multiple files to be uploaded via one INPUT element.  If you're a stickler for validation, you'll want to assign the multiple attribute a value of multiple.

Listing Multiple Files with JavaScript

//get the input and UL list
var input = document.getElementById('filesToUpload');
var list = document.getElementById('fileList');

//empty list for now...
while (list.hasChildNodes()) {
	list.removeChild(ul.firstChild);
}

//for every file...
for (var x = 0; x < input.files.length; x++) {
	//add to list
	var li = document.createElement('li');
	li.innerHTML = 'File ' + (x + 1) + ':  ' + input.files[x].name;
	list.append(li);
}

The input.files property provides an array of files for which you can check the length;  if there's a length, you can loop through each file and access the file paths and names.

Receiving and Handling Files with PHP

if(count($_FILES['uploads']['filesToUpload'])) {
	foreach ($_FILES['uploads']['filesToUpload'] as $file) {
	    
		//do your upload stuff here
		echo $file;
		
	}
}

PHP creates an array of the files uploaded with the given INPUT's name.  This variable will always be an array within PHP.

Of course, you could a Flash-based equivalent for Internet Explorer and Opera, but having to add and support another component can be taxing.  Hopefully these browsers add support for multiple file uploads soon!

Discussion

  1. July 19, 2010 @ 9:04 am

    Great thanks! I am always running into this problem project after project. I have been using http://www.uploadify.com/demo/ for IE users (flash alternative), it’s very easy to implement and customise (event handlers etc).

  2. July 19, 2010 @ 9:19 am

    This is the only reason I ever use flash on a page. The lack of progress sadly still means it is needed, but I don’t see that being fixed anytime soon.

  3. July 19, 2010 @ 9:33 am

    Awesome! Thanks for sharing this little gem! It would make a welcome addition to certain modules in our in-house CMS.

  4. July 19, 2010 @ 10:02 am

    didn’t work in last opera

  5. andy
    July 19, 2010 @ 5:53 pm

    Doesn’t work in IE(8) or Opera. It would be helpful if you would add what browsers your tips work in, some Googler might find this page and later down the troubleshooting line get confused when it doesn’t work in a different browser.

  6. July 19, 2010 @ 6:11 pm

    fuck, nice! i dont know about this, because I use opera always! ;)

  7. July 19, 2010 @ 8:02 pm

    @Andy: Did you read the post? “Luckily Safari, Chrome, and Firefox have implemented a method by which users can upload multiple files within one INPUT element.”

    Please read the post next time.

  8. dmitry
    July 20, 2010 @ 2:03 am

    Thanks David.

    This problem with upload arises from project to project. In my last project I tried http://www.aurigma.com/Products/ImageUploaderFlash/, it worked good enough with all browsers.

  9. July 20, 2010 @ 3:33 am

    The usual comments from people who don’t read a post (i.e. Dr Death and Andy).

    Not only do you say “Luckily Safari, Chrome, and Firefox have implemented a method by which users can upload multiple files within one INPUT element” but at the end you also say “Of course, you could a Flash-based equivalent for Internet Explorer and Opera, but having to add and support another component can be taxing. Hopefully these browsers add support for multiple file uploads soon!”

    I’m not sure how this at all confusing about the current state of browser support…

  10. July 20, 2010 @ 3:36 am

    The usual comments from people who don’t know how to read a post (i.e. Dr Death and Andy).

    Not only do you (David) say “Luckily Safari, Chrome, and Firefox have implemented a method by which users can upload multiple files within one INPUT element” but at the end you also say “Of course, you could a Flash-based equivalent for Internet Explorer and Opera, but having to add and support another component can be taxing. Hopefully these browsers add support for multiple file uploads soon!”

    I’m not sure how this at all confusing about the current state of browser support…

  11. July 20, 2010 @ 3:59 am

    Hey David, can you delete this one, and my first (or second) comment.., Something went wrong when posting (took ages) and I posted the comment again and they both ended up on this page. Thanks!

  12. kburn
    July 20, 2010 @ 8:20 am

    AWSUM!!

    It would be nice to find out a way to implement an uploading bar, to show the progress. ;)

  13. chris brewer
    July 20, 2010 @ 9:53 am

    Way cool. Thanks David. Would be super excellent to combine this capability with the capabilities of this script (http://the-stickman.com/files/mootools/multiupload/) in terms of removing the files from the queue of files to upload…

  14. July 21, 2010 @ 3:21 am

    This multiple upload thing should be a must-have-feature in modern browsers!

  15. jamesh
    July 21, 2010 @ 5:23 am

    Pretty cool, I wonder how long until ‘The Other’ browser implements something like this. Being limited to a single file per INPUT is a pain in the arse, and while the Flash alternatives solve the problem, I do not like having to use Flash to resolve a problem that shouldn’t of existed in the first place..but oh well, it’s the standard.

    Thanks for the heads up David!

  16. July 23, 2010 @ 8:57 am

    Thanks David,
    I’m going to add this feature to our web-apps it’s great. Next one is web-browser based drop-box’s straight from the desktop.

  17. July 25, 2010 @ 12:34 am

    Great article!

    Just wanted to mention: if you’re working with an XHTML doctype, then you’ll need to use multiple=”multiple”. Otherwise, multiple by itself will work. Same rule applies to all boolean attributes.

  18. July 29, 2010 @ 2:21 am

    Hi nice article.

    Thanks
    Addy

  19. July 29, 2010 @ 2:23 pm

    cannot believe this is possible I tried everything, especiallly the Flash is awfull with loosing its session in everything but IE.
    Thank you for sharing, If it works , I will take you out in Amsterdam till you drop, I mean it !

  20. ncc50446
    July 29, 2010 @ 5:41 pm

    If you are an Opera user, you can use this

    Opera has had it since Opera 6
    Just need to check their user agent and display the right one. A pain, but still doable.

  21. ncc50446
    July 29, 2010 @ 5:44 pm

    Ok…Lets try that again lol
    Just put in the opening and closing html tags

    input type=”file” name=”filesToUpload[]” id=”filesToUpload[]” min=”1″ max=”9999″ /

  22. aldian
    August 4, 2010 @ 4:50 pm

    Google docs has added new upload feature. It uses multiple file input and also works in IE eventhough have disabled flash. How did they manage it to work in IE?

  23. marcelo barros
    August 9, 2010 @ 1:48 pm

    Please David, Can you share the complete code of this example? :D

    Thanks

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!