我想计算上传文件集。这是我的使用代码。有没有更好的方法来做到这一点。感谢。
<form action="index.php" method="post" enctype="multipart/form-data">
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<button name="submit" type="submit">Upload</button>
</form>
<?php
$img_error = '0';
$fill_img_count = '0';
if(isset($_POST['submit']))
{
$img_count = count($_FILES['new_image']);
echo "Total : ".$img_count."<br>";
for ($i=0 ; $i<=$img_count ; $i++)
{
if (isset($_FILES['new_image']) && !empty($_FILES['new_image']['name'][$i]))
{
$fill_img_count++;
}
}
echo "Set : ".$fill_img_count."<br>";
}
?>
答案 0 :(得分:2)
$count_files = 0;
foreach ($_FILES['picture']['error'] as $item) {
if ($item != 4) {
$count_files++;
}
}
echo $count_files;
答案 1 :(得分:1)
我建议针对UPLOAD_ERR_OK测试每个['error']
密钥。
答案 2 :(得分:0)
您不需要name="new_image[]"
作为名称......只需要new_image即可。如果您发布1或多个,在PHP端,您将看到$ _FILES []
一些有用的链接:
一些代码:
if (empty($_FILES)) { echo "0 files uploaded"; }
else { echo count($_FILES) . " files uploaded"; }
根据评论修改:
从那篇文章:
echo count($_FILES['file']['tmp_name']);
答案 3 :(得分:0)
<?php
$count = 0;
foreach($_FILES['new_image']['error'] as $status){
if($status === UPLOAD_ERR_OK) {
$count++;
}
}
var_dump($count);
?>
<form action="test.php" method="post" enctype="multipart/form-data">
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<button name="submit" type="submit">Upload</button>
</form>