简体   繁体   English

PHP超全局数组的长度

[英]Length of a PHP superglobal array

是否可以使用某种length()或size()检查$ _FILES []中有多少个项目?

It's a normal array besides the fact that it's superglobal - simply use count($_FILES) . 除了它具有超全局性之外,它是一个普通数组-只需使用count($_FILES)

To count the successfully uploaded files, you could do the following in PHP5.3: 要计算成功上传的文件,可以在PHP5.3中执行以下操作:

$successCount = array_reduce($_FILES, function($val, $file) {
    if($file['error'] == UPLOAD_ERR_OK) return $val + 1;
    return $val;
}, 0);

In older PHP versions the easiest way (I consider string function names for callbacks ugly) would be using a simple loop: 在较早的PHP版本中,最简单的方法(我认为回调的字符串函数名很丑陋)将使用一个简单的循环:

$count = 0;
foreach($_FILES as $file) {
    if($file['error'] == UPLOAD_ERR_OK) $count++;
}

count()函数可以做到这一点: http : //php.net/manual/en/function.count.php

$c = count($_FILES);

there are 2 ways in naming fields for the upload. 上传字段的命名方式有两种。
While filename[] will make this array messy, filename1 , filename2 etc will make $_FILES array behave as you expected and count($_FILES) will return number of input fields 尽管filename[]会使此数组混乱,而filename1 ,, filename2等会使$ _FILES数组的行为符合预期,而count($ _ FILES)将返回输入字段的数量

If you don't know array structure, you should use print_r($_FILES) to see it first and then decide, what you want to count. 如果您不知道数组结构,则应使用print_r($_FILES)首先查看它,然后再决定要计数的内容。

Also note that array name is $_FILES , not $_FILES[] as you mentioned. 还要注意,数组名是$_FILES ,而不是您提到的$_FILES[] It's operator, not array name. 它是运算符,而不是数组名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM