Length of a PHP superglobal array
Is it possible to check how many items are within $_FILES[] using some sort of length() 开发者_运维百科or size() ?
It's a normal array besides the fact that it's superglobal - simply use count($_FILES)
.
To count the successfully uploaded files, you could do the following in 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:
$count = 0;
foreach($_FILES as $file) {
if($file['error'] == UPLOAD_ERR_OK) $count++;
}
The count() function does that: 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
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.
Also note that array name is $_FILES
, not $_FILES[]
as you mentioned. It's operator, not array name.
精彩评论