Multiple File Upload results in foreach() error

丶灬走出姿态 提交于 2019-12-11 06:32:28

问题


Actually trying to upload multiple pictures in a folder at once with some kind of renaming. The upload goes to the foreach loop. However, it does stop at this point:

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);
            }
        }
    }

However, I receive this error in the error_log and the files aren't moved:

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

UPDATE: Here is the HTML Code:

<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>

Maybe you guys has any Idea how to have this resolved?

Thanks :)


回答1:


Replace:

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

with:

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



回答2:


Just needed to add [] at the end of the form input "name".



来源:https://stackoverflow.com/questions/45251111/multiple-file-upload-results-in-foreach-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!