从laravel 5.2中的多个文件输入上传多个文件

时间:2016-10-30 17:30:13

标签: php file-upload laravel-5.2

我尝试制作动态表单,允许用户上传文件,以便每个图像都有视频,他们应该上传并将其路径保存到数据库但是当我想在我的控制器中获取文件并将它们存储在数组中时我看到了我上传了一个或两个有效的文件但是当我尝试上传超过3或4个文件时,我的数组变空了,我无法解决它,请帮我完成我的项目。非常感谢。

这是我的html表单:

    <form action="admin/mediaUpload" method="post" enctype="multipart/form-data" id="mediaUpload">
     <table class="table table-bordered table-hover" id="tableAddRow">
                            <thead>
                            <tr>
                                <th>image</th>
                                <th>film</th>
                                <th style="width:10px"><span class="glyphicon glyphicon-plus addBtn" id="addBtn_0"></span></th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr id="tr_0">
                                <td><input type="file" id="image_0" name="image-0" class="form-control"/></td>
                                <td><input type="file" id="video_0" name="video-0" class="form-control" /></td>
                                <td style="background-color: white;"><span class="glyphicon glyphicon-minus addBtnRemove" style="margin-top: 9px;" id="addBtnRemove_0"></span></td>
                            </tr>
                            </tbody>
                        </table>

                        <input type="submit" name="submit" id="mediaSubmit" value="بارگذاری" class="btn btn-primary" style="float: right; width: 40%"/><br/><br/>
                    </form>

这是我的控制者:

public function mediaUpload(uploadRequest $uploadRequest)
{
    $data = $uploadRequest->all();
    $numberId = $uploadRequest['numberId'];
    $rowCount = $uploadRequest['rowCount'];

    $imageArray = [];
    $videoArray = [];

    for($i = 0 ; $i<$rowCount - 1 ;$i++)
    {
        $imageArray[$i] = Input::file('image-'.$i);
    }

    for($i = 0 ; $i<$rowCount - 1 ;$i++)
    {
        $videoArray[$i] = Input::file('video-'.$i);
    }

    var_dump($imageArray);
    var_dump($videoArray);
}

1 个答案:

答案 0 :(得分:0)

您可以在输入中使用数组:

<tr id="tr_0">
    <td><input type="file" id="image_0" name="image[]" class="form-control"/></td>
    <td><input type="file" id="video_0" name="video[]" class="form-control" /></td>
</tr>
<tr id="tr_1">
    <td><input type="file" id="image_1" name="image[]" class="form-control"/></td>
    <td><input type="file" id="video_1" name="video[]" class="form-control" /></td>
</tr>
<tr id="tr_2">
    <td><input type="file" id="image_2" name="image[]" class="form-control"/></td>
    <td><input type="file" id="video_2" name="video[]" class="form-control" /></td>
</tr>

然后循环请求属性:

if (Input::hasFile('video')) {
    foreach (Input::file('video') as $video) {
        // do something...
    }
}

image

的情况相同
相关问题