如何在laravel中将图像上传到数据库中

时间:2017-03-25 12:18:04

标签: mysql image laravel

我已经为multipleupload添加了我的代码:

public function upload(Request $request) {

     $image = $request->file('file')->getClientOriginalName();

    $filename = time().$image;
    if(Input::hasFile('file')){

        echo 'uploaded <br/>';
        $file = Input::file('file');
        $file->move('upload', $filename);
        echo '<img src="upload/' . $filename .'"/>';
    }

   $gallery = new Gallery;
   $gallery->image = $filename;
   $gallery->save();
}

视图:

<form action="uploaded" method="post"  enctype="multipart/form-data">

    {{csrf_field()}}

    <div class="form-group">
        <input type="file" class="form-group" name="file[]" multiple>
    </div>
    <button type="submit" class="btn btn-primary">Add</button>
</form>

1 个答案:

答案 0 :(得分:0)

虽然我不知道您的问题是什么,但我认为您应该将Input更改为$request

像这样:

public function upload(Request $request){

    $image = $request->file('file')->getClientOriginalName();

    $filename = time().$image;
    if($request->hasFile('file')){

       echo 'uploaded <br/>';
       $file = request->file('file');
       $file->move('upload', $filename);
       echo '<img src="upload/' . $filename .'"/>';
    }

   $gallery = new Gallery;
   $gallery->image = $filename;
   $gallery->save();
}