Laravel 5.2 - MultiUpload输入

时间:2016-10-27 14:47:42

标签: php laravel file-upload

我希望我的用户可以上传4张图片,这是我第一次使用类似功能,但我不知道怎么做?创造阵列?我想要更多的输入,我不想使用多选文件,我试试这样:

表格HTML:

{!! Form::open(array('url'=>'crea-regalo','method'=>'POST','class' => 'form-horizontal', 'files'=>true)) !!}

<!-- photo -->
<div class="form-group">
   <label class="col-md-3 control-label" for="textarea"> Picture </label>
   <div class="col-md-8">

    <div class="mb10">

     <input id="input-upload-img1" name="image" type="file" class="file" data-preview-file-type="text">
   </div>

   <div class="mb10">

     <input id="input-upload-img2" name="image" type="file" class="file" data-preview-file-type="text">
   </div>


   <div class="mb10">

     <input id="input-upload-img3" name="image" type="file" class="file" data-preview-file-type="text">
   </div>


   <div class="mb10">

     <input id="input-upload-img4" name="image" type="file" class="file" data-preview-file-type="text">
   </div>

{!! Form::close() !!}

CONTROLLER - 实际上我只有一张图片的代码 - 效果很好

public function creaPost(Request $request){
         // get id of post created by user
         $get_post_created = Post::where('slug', str_slug($request->input('title'),'-'))->first();
         // set file uploaded
         $file = $request->file('image');
         // create instance image intervation
         $image = image::make($request->file('image'));
         // create path
         $path = public_path().'/images/post/';
         // rename image with id user - if more one i can change this myself
         $name_file = $get_post_created->id . '.' . $request->file('image')->getClientOriginalExtension();

        //resize
        $image->resize(100,100);
        // save image
        $image->save($path.$name_file);
        // update post with image uploaded
        $user = new Post;
        $user->where('id', '=', $get_post_created->id)->update(['image' => 'images/post/'.$name_file]);
        return redirect('/')->with('message-success', 'Post creato con successo!');
}

1 个答案:

答案 0 :(得分:0)

只需将[]添加到输入名称:

<input id="input-upload-img1" name="image[]" type="file" class="file" data-preview-file-type="text">

或者:

{!! Form::file('image[]') !!}

您也可以删除4个输入并添加此输入,这样用户就可以在一个文件浏览器窗口中选择4个图像:

{!! Form::file('image[]', ['multiple' => true]) !!}

在控制器中迭代数组以获取每个文件:

foreach ($request->image as $image)