文件输入问题 - 限制所选文件的数量 [英] File input problems - Limit number of selected files

查看:179
本文介绍了文件输入问题 - 限制所选文件的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



1)首先,我有一个< input现在我可以根据需要选择尽可能多的图片,但是我需要将这个数字限制为最大10。我怎样才能做到这一点?



2)我的upload.php文件看起来像处理多个上传的图片?我从来没有机会单独上传图片,但现在我坚持这一点,我需要这个。我很困惑...

请帮忙吗?

解决方案

1您可以使用javascript来检测所选文件的数量,如果超过10, >

  $('fileinput')。onchange = function(){if(this.files.length> 10)alert('to many files ')} 
//防止提交if
$('form')。onsubmit = function(){if(this.files.length> 10)return false;}

你甚至可以通过加入所有.files [i] .fileSize来检查组合文件大小是否不大。 p>

<2>请参阅: http ://php.net/manual/en/features.file-upload.multiple.php
(短版本;使用:$ _FILES ['userfile'] ['name'] [0], $ _FILES ['userfile'] ['tmp_name'] [0],$ _FILES ['userfile'] ['size'] [0]和$ _FILES ['userfile'] ['type'] [0])


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?

解决方案

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

这篇关于文件输入问题 - 限制所选文件的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆