【问题标题】:PHP file upload count set filePHP文件上传计数设置文件
【发布时间】:2011-07-15 12:45:56
【问题描述】:

我想统计设置文件上传。这是我的使用代码。有没有更好的方法来做到这一点。谢谢。

<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>";
}
?>

【问题讨论】:

    标签: php file upload count


    【解决方案1】:
    $count_files = 0;
    foreach ($_FILES['picture']['error'] as $item) {
        if ($item != 4) {
            $count_files++;
        }
    }
    echo $count_files;
    

    【讨论】:

      【解决方案2】:

      我建议针对 UPLOAD_ERR_OK 测试每个 ['error'] 键。

      【讨论】:

        【解决方案3】:

        您不需要将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']);
        

        【讨论】:

        • 如果发送空文件字段,此代码将失败,这是 OP 明确希望避免的。
        【解决方案4】:
        <?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>
        

        【讨论】:

          猜你喜欢
          • 2018-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-23
          相关资源
          最近更新 更多