沃梦达 / 编程问答 / php问题 / 正文

统计和限制上传文件的数量(HTML文件输入)

Count and limit the number of files uploaded (HTML file input)(统计和限制上传文件的数量(HTML文件输入))

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

问题描述

我有一个基本的、众所周知的多文件上传表单.类似的东西...

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.

提前致谢.:)

推荐答案

您可以实现一个 javascript 解决方案来检查已选择的文件数量,然后您可以使用它来禁止上传到服务器.不过,对于这个问题,实际上只有客户端解决方案,因为实际上没有什么可以阻止用户将这些文件发布到您的 php 脚本中.您可以指定最大上传大小限制,但它并不特定于正在上传的文件数量.

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 检查,为您的 http 服务器(或在 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.

在此处阅读 HTML5 文件 API 以限制所选文件的数量:http://dev.w3.org/2006/webapi/FileAPI/

Read about the HTML5 File API here to restrict the count of selected files: http://dev.w3.org/2006/webapi/FileAPI/

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

Here is the php.ini docs which explain how to make a size limitation on uploads: http://php.net/manual/en/ini.core.php

正如评论中所建议的,请查看:http://php.net/manual/en/ini.core.php#ini.max-file-uploads

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

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

本文标题为:统计和限制上传文件的数量(HTML文件输入)

基础教程推荐