计算并限制上传的文件数(HTML文件输入)

Bik*_*ohn 22 html php file-upload

我有这个基本的,众所周知的多文件上传表单.这样的东西......

<input type="file" multiple="multiple" name="something[]" />
Run Code Online (Sandbox Code Playgroud)

有没有办法(最好是硬编码选项)来限制用户可以选择的文件数量?限制我的意思是严格阻止,而不仅仅是超过数字的警告.

提前致谢.:)

Gol*_*wby 9

您可以实现一个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-file-uploads

  • `因为没有什么能阻止用户将这些文件发布到你的php脚本中:事实上,有.在`php.ini`中,您可以使用`max_file_uploads`选项限制该数字(http://www.php.net/manual/en/ini.core.php#ini.max-file-uploads) (8认同)
  • @marvin:请将其添加为答案 (2认同)

Rit*_*tam 7

我们都知道在一个数组中 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
}
Run Code Online (Sandbox Code Playgroud)