aborted
aborted

Reputation: 4541

File input problems - Limit number of selected files

I'm putting two questions in this "Question"...

1) First of all, I have a <input type="file" name="file[]" multiple="multiple" />, Now I can select as many images as I want, but I need to limit this number to 10 maximally. How would I do this?

2) What would my upload.php file look like to handle multiple uploaded images? I never had the chance to work on anything more than single image uploading, but now I'm stuck at this and I need this. I'm quite confused...

Help please?

Upvotes: 3

Views: 7657

Answers (3)

For the front-end, you should also consider using file upload libraries: they allow limiting and much more:

They are also available at https://cdnjs.com/

Upvotes: 0

RbG
RbG

Reputation: 3193

I have already given an answer

so i am writing only the code part here

if(empty($_FILES['file']['name'][0]))
{
     //checking whether a single file uploaded or not
     //if enters here means no file uploaded
}
if(isset($_FILES['file']['name'][10]))
{
     //checking whether 11 files uploaded or not
     //so here you can restrict user from uploading more than 10 files
}

Upvotes: 0

Gerben
Gerben

Reputation: 16825

1 You could use javascript to detect the number of files selected, and give a warning if it's more than 10

$('fileinput').onchange=function(){if(this.files.length>10)alert('to many files')}
//prevent submitting if to many
$('form').onsubmit=function(){if(this.files.length>10)return false;}

you could even check if the combined filesize isn't to big by adding up all .files[i].fileSize

2 see: http://php.net/manual/en/features.file-upload.multiple.php (short version; use: $_FILES['userfile']['name'][0], $_FILES['userfile']['tmp_name'][0], $_FILES['userfile']['size'][0], and $_FILES['userfile']['type'][0])

Upvotes: 4

Related Questions