php 统计并限制上传文件的数量(HTML文件输入)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9813556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count and limit the number of files uploaded (HTML file input)
提问by Biker John
I have that basic, well known multiple file upload form. Something like that...
我有那个基本的、众所周知的多文件上传表单。类似的东西...
<input type="file" multiple="multiple" name="something[]" />
Is there a way (preferable a hard coded option) to limit the number of files that user can select? By limit i mean strictly blocked, not just a warning if number is exceeded.
有没有办法(最好是硬编码选项)来限制用户可以选择的文件数量?通过限制,我的意思是严格阻止,而不仅仅是超出数量的警告。
Thanks in advance. :)
提前致谢。:)
采纳答案by GoldenNewby
You can implement a javascript solution to check the number of files that have been selected, which you can then use to forbid uploading to the server. There are really only going to be client-side solutions to this problem though, as there is really nothing stopping a user from posting these files to your php script anyway. You can specify a maximum upload size limit, but it isn't specific to the number of files that are being uploaded.
您可以实现一个 javascript 解决方案来检查已选择的文件数量,然后您可以使用它来禁止上传到服务器。不过,对于这个问题,实际上只有客户端解决方案,因为实际上没有什么可以阻止用户将这些文件发布到您的 php 脚本中。您可以指定最大上传大小限制,但它并不特定于正在上传的文件数。
Your best bet is to implement some javascript checking, specifying a reasonable maximum upload size for your http server (or in PHP), and then ignoring any file uploaded if it exceeds your maximum count.
最好的办法是执行一些 javascript 检查,为您的 http 服务器(或在 PHP 中)指定一个合理的最大上传大小,然后忽略任何超过您的最大数量上传的文件。
Read about the HTML5 File API here to restrict the count of selected files: http://dev.w3.org/2006/webapi/FileAPI/
在此处阅读 HTML5 文件 API 以限制所选文件的数量:http: //dev.w3.org/2006/webapi/FileAPI/
Here is the php.ini docs which explain how to make a size limitation on uploads: http://php.net/manual/en/ini.core.php
这是 php.ini 文档,它解释了如何对上传进行大小限制:http: //php.net/manual/en/ini.core.php
As was suggested in the comments, check out: http://php.net/manual/en/ini.core.php#ini.max-file-uploads
正如评论中所建议的,请查看:http: //php.net/manual/en/ini.core.php#ini.max-file-uploads
回答by Ritabrata Gautam
we all know that in an array first element is stored in 0 th index, 2nd in 1st index......nth in (n-1)th index
我们都知道在数组中 first element is stored in 0 th index, 2nd in 1st index......nth in (n-1)th index
now you cant take count($_FILES['something'])
which will always return 5 since there are 5 keys in the array
现在你不能接受count($_FILES['something'])
它总是返回 5,因为数组中有 5 个键
now the question is what does $_FILES['something']['name'][0]
contains? =>name of first file uploaded
现在的问题是$_FILES['something']['name'][0]
包含什么?=>上传的第一个文件的名称
so check like
所以检查像
if(empty($_FILES['something']['name'][0]))
{
//checking whether a single file uploaded or not
//if enters here means no file uploaded
//so if you want you may put a check/validation here to make file
//upload a must in your website
}
if(isset($_FILES['something']['name'][5]))
{
//checking whether 6 files uploaded or not
//so here you can put a check/validation to restrict user from
//uploading more than 5 files
}
回答by mikevoermans
Some plugins seem to assist with this, such as: Uploadify (flash based). However I haven't used this one or others enough to know if they how restricting they can limit uploads.
一些插件似乎对此有所帮助,例如:Uploadify (flash based)。但是,我还没有使用过这个或其他的足够了解他们是否可以限制上传。
More info on Uploadify Multiple Uploads.
有关Uploadify Multiple Uploads 的更多信息。