计数并限制上传的文件数(HTML文件输入) [英] Count and limit the number of files uploaded (HTML file input)

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

问题描述

我有这个基本的,众所周知的多文件上传形式。像这样的东西...

 < input type =filemultiple =multiplename =something [] /> 

有没有一种方法(最好是硬编码选项)来限制用户可以选择的文件数量?通过限制我的意思是严格封锁,不只是一个警告,如果数量超过。



在此先感谢。 :)

解决方案

你可以实现一个javascript解决方案来检查已经被选择的文件的数量,然后你可以使用禁止上传到服务器。这个问题真的只有客户端解决方案,因为没有任何东西阻止用户将这些文件发布到您的PHP脚本。你可以指定一个最大的上传大小的限制,但它并不特定于正在上传的文件的数量。

你最好的选择是实现一些JavaScript检查,为您的http服务器(或PHP)指定一个合理的最大上传大小,然后忽略上传的任何文件,如果它超过了您的最大数量。

阅读关于HTML5 File API这里限制所选文件的数量: http://dev.w3.org/2006/webapi / FileAPI /

下面是php.ini文档,它解释了如何对上传进行大小限制: http://php.net/manual/en/ini.core.php

正如在评论中提出的,看看: http://php.net/manual/en/ini.core.php#ini.max-file-uploads


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. :)

解决方案

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.

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.

Read about the HTML5 File API here to restrict the count of selected files: 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

As was suggested in the comments, check out: http://php.net/manual/en/ini.core.php#ini.max-file-uploads

这篇关于计数并限制上传的文件数(HTML文件输入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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