开发者

File input problems - Limit number of selected files

开发者 https://www.devze.com 2023-03-17 01:21 出处:网络
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

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. Ho开发者_运维百科w 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?


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])


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
}


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

  • FineUploader. Demo

  • bluimp's jQuery File Upload Plugin. Usage: jquery file upload restricting number of files

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

0

精彩评论

暂无评论...
验证码 换一张
取 消