186

How to select multiple files with <input type="file">?

approxiblue
  • 6,982
  • 16
  • 51
  • 59
Mask
  • 33,129
  • 48
  • 101
  • 125
  • It's about javascript and html – Mask Oct 20 '09 at 08:47
  • Do you mean upload multiple files in one go (when you select one at a time and then click upload)? Or do you mean using ctrl+click to select several files in one browser window? – cletus Oct 20 '09 at 09:04
  • 4
    You can do it with HTML5 using the multiple attribute on the input element. Here's a great fiddle that utilizes it: http://jsfiddle.net/0GiS0/Yvgc2/ – Costa Michailidis Jul 05 '13 at 22:34

11 Answers11

224

New answer:

In HTML5 you can add the multiple attribute to select more than 1 file.

<input type="file" name="filefield" multiple="multiple">

Old answer:

You can only select 1 file per <input type="file" />. If you want to send multiple files you will have to use multiple input tags or use Flash or Silverlight.

ZippyV
  • 12,540
  • 3
  • 37
  • 52
  • 8
    Gmail uses Flash to do this – Fabien Ménager Oct 20 '09 at 08:52
  • 2
    @Costa On the 20th of October of 2009 most browsers didn't support that feature and Niavlys already showed the html5 solution 2 years ago. – ZippyV Jul 05 '13 at 23:40
  • 7
    this answer is more ancient than the dinosaurs – Ali Aug 30 '13 at 21:44
  • The HTML5 multiple attribute is supported on current versions of all major browsers. See: http://caniuse.com/#search=multiple (N.B. if you're managing support for older browsers, continue to be aware of their support too) – Jeremy Lawson May 01 '16 at 23:29
  • 2
    this multiple="multiple" is not user friendly, the avarage user doesn't understand it, doesn't even know what a "ctrl button" does, and it cannot select files in different folders. – Jean-Paul Jan 30 '17 at 16:04
  • 6
    if this is form is being submitted to a php form name should be `name="filefield[]"` so that it returns an array – But those new buttons though.. Feb 06 '18 at 20:24
  • 1
    @Jean-Paul This solution is *especially* user-friendly, because it can *also* be used to add files one at a time (if a user doesn't know how to select multiple files). Allowing to use the field multiple times, if necessary, is your job while implementing the form. – Thomas Ebert Apr 02 '19 at 12:44
  • As a side not, selected files info can be accessed via `files` property on the input element. – Alireza Aug 14 '20 at 11:27
96

There is also HTML5 <input type="file" multiple name="files[]" /> (specification).

Browser support is quite good on desktop (just not supported by IE 9 and prior), less good on mobile, I guess because it's harder to implement correctly depending on the platform and version.

sylbru
  • 1,461
  • 1
  • 11
  • 19
33

The whole thing should look like:

<form enctype='multipart/form-data' method='POST' action='submitFormTo.php'> 
    <input type='file' name='files[]' multiple>
    <button type='submit'>Submit</button>
</form>

Make sure you have the enctype='multipart/form-data' attribute in your <form> tag, or you can't read the files on the server side after submission!

user3840170
  • 26,597
  • 4
  • 30
  • 62
mark.inman
  • 2,521
  • 2
  • 18
  • 12
17

This jQuery plugin (jQuery File Upload Demo) does it without flash, in the form it's using:

<input type='file' name='files[]' multiple />
Peter O.
  • 32,158
  • 14
  • 82
  • 96
DigitalDaigor
  • 432
  • 3
  • 12
  • also not supported on IE9-, https://github.com/blueimp/jQuery-File-Upload/wiki/Browser-support#multiple-file-selection – JerryDDG Nov 26 '14 at 10:44
15

You can do it now with HTML5

In essence you use the multiple attribute on the file input.

<input type='file' multiple>
Costa Michailidis
  • 7,691
  • 15
  • 72
  • 124
3

Thats easy ...

<input type='file' multiple>
$('#file').on('change',function(){
     _readFileDataUrl(this,function(err,files){
           if(err){return}
           console.log(files)//contains base64 encoded string array holding the 
           image data 
     });
});
var _readFileDataUrl=function(input,callback){
    var len=input.files.length,_files=[],res=[];
    var readFile=function(filePos){
        if(!filePos){
            callback(false,res);
        }else{
            var reader=new FileReader();
            reader.onload=function(e){              
                res.push(e.target.result);
                readFile(_files.shift());
            };
            reader.readAsDataURL(filePos);
        }
    };
    for(var x=0;x<len;x++){
        _files.push(input.files[x]);
    }
    readFile(_files.shift());
};
2

Just use multiple attribute

<input type='file' multiple>

Read more about multiple attribute

Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66
1

HTML5 has provided new attribute multiple for input element whose type attribute is file. So you can select multiple files and IE9 and previous versions does not support this.

NOTE: be carefull with the name of the input element. when you want to upload multiple file you should use array and not string as the value of the name attribute.

ex: input type="file" name="myPhotos[]" multiple="multiple"

and if you are using php then you will get the data in $_FILES and use var_dump($_FILES) and see output and do processing Now you can iterate over and do the rest

Arjun J Gowda
  • 720
  • 10
  • 21
0

HTML5 has provided new attribute multiple for input element whose type attribute is file. So you can select multiple files and IE9 and previous versions does not support this.

NOTE: be carefull with the name of the input element. when you want to upload multiple file you should use array and not string as the value of the name attribute.

ex:

input type="file" name="myPhotos[]" multiple="multiple"

and if you are using php then you will get the data in $_FILES and use var_dump($_FILES) and see output and do processing Now you can iterate over and do the rest

Cristik
  • 30,989
  • 25
  • 91
  • 127
-1
<form action="" method="post" enctype="multipart/form-data">
<input type="file" multiple name="img[]"/>
<input type="submit">
</form>
<?php
print_r($_FILES['img']['name']);
?>
Anowar Hossain
  • 583
  • 4
  • 17
-2

Copy and paste this into your html:

<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>

<script>
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object

// files is a FileList of File objects. List some properties.
var output = [];
for (var i = 0, f; f = files[i]; i++) {
  output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
              f.size, ' bytes, last modified: ',
              f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
              '</li>');
}
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
}

This comes to you, through me, from this webpage: http://www.html5rocks.com/en/tutorials/file/dndfiles/

collyg
  • 171
  • 1
  • 7