多个文件上载导致foreach()错误

时间:2017-07-22 05:41:18

标签: php foreach image-uploading

实际上尝试使用某种重命名方式一次上传文件夹中的多张图片。上传进入foreach循环。但是,它确实在此时停止:

foreach ($_FILES['pictures']['name'] as $f => $name) {
        if ($_FILES['pictures']['error'][$f] == 0) {
            $name = $date;
            $name .= "-";
            $name .= $i;
            $name .= ".";
            $extsearch = $_FILES['pictures']['name'][$f];
            $ext = pathinfo($extsearch, PATHINFO_EXTENSION);
            $name .= $ext;
            if(move_uploaded_file($_FILES["pictures"]["tmp_name"][$f], $path.$name)) {
                if ($i == 1) {
                    create_image_thumb($albumFolder, $name);
                }
                $i++;
                resize_image($albumFolder, $name);
            }
        }
    }

但是,我在error_log中收到此错误,并且文件未被移动:

PHP Warning:  Invalid argument supplied for foreach() in /home/clublabo/public_html/management/create_album_action.php on line 43

更新:这是HTML代码:

<form class="form form-horizontal" action="create_album_action.php" method="post" enctype="multipart/form-data">
              <div class="form-group">
                <label for="email-2" class="col-sm-3 col-md-4 control-label">Date</label>
                <div class="col-sm-6 col-md-4">
                  <div class="input-with-icon">
                  <input name="dateAlbum" class="form-control" type="text" value="<?php echo date('Y-m-d'); ?>" data-provide="datepicker" data-date-autoclose="true" data-date-format="yyyy-mm-dd"><span class="icon icon-calendar input-icon"></span></div>
                </div>
              </div>
              <div class="form-group">
                <label for="password-2" class="col-sm-3 col-md-4 control-label">Pictures</label>
                <div class="col-sm-6 col-md-4">
                  <input name="pictures" id="password-2" class="form-control" type="file" multiple>
                </div>
              </div>
              <div class="form-group">
                <div class="col-sm-offset-3 col-sm-6 col-md-offset-4 col-md-4">
                  <button class="btn btn-primary pull-right" type="submit">Create Album</button>
                </div>
              </div>
            </form>

也许你们有任何想法如何解决这个问题?

谢谢:)

2 个答案:

答案 0 :(得分:1)

只需在表单输入“name”的末尾添加[]。

答案 1 :(得分:1)

替换:

<input name="pictures" id="password-2" class="form-control" type="file" multiple>

使用:

<input name="pictures[]" id="password-2" class="form-control" type="file" multiple>