统计并限制上载的文件数(HTML文件输入)


Count and limit the number of files uploaded (HTML file input)

我有一个基本的、众所周知的多文件上传表单。。。

<input type="file" multiple="multiple" name="something[]" />

是否有一种方法(最好是硬编码选项)来限制用户可以选择的文件数量?我所说的"限制"是指严格阻止,而不仅仅是在数量超过时发出警告。

提前感谢。:)

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

最好的办法是实现一些javascript检查,为http服务器(或PHP)指定一个合理的最大上传大小,然后在超过最大数量时忽略任何上传的文件。

在此处阅读有关HTML5文件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-文件上传

我们都知道,在数组first element is stored in 0 th index, 2nd in 1st index......nth in (n-1)th index

现在你不能接受count($_FILES['something']),因为数组中有5个键,它总是返回5

现在的问题是$_FILES['something']['name'][0]包含什么=>上传的第一个文件的名称

所以像一样检查

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
}

能做到这一点的纯Js替代品以及更多:

  • FineUploader。演示

  • bluimp的jQuery文件上传插件。用法:jquery文件上传限制文件数

它们也可在https://cdnjs.com/

一些插件似乎有助于实现这一点,例如:Uploadify(基于flash)。然而,我还没有使用这个或其他足够的工具来了解他们是否可以限制上传。

关于上传多个上传的更多信息。