send image by form in laravel

Solutions on MaxInterview for send image by form in laravel by the best coders in the world

showing results for - "send image by form in laravel"
Ela
14 Mar 2016
1<form enctype="multipart/form-data" method="post" action="{{url('admin/post/insert')}}">
2    {{ csrf_field() }}
3    <div class="form-group">
4        <label for="imageInput">File input</label>
5        <input data-preview="#preview" name="input_img" type="file" id="imageInput">
6        <img class="col-sm-6" id="preview"  src="">
7        <p class="help-block">Example block-level help text here.</p>
8    </div>
9    <div class="form-group">
10        <label for="">submit</label>
11        <input class="form-control" type="submit">
12    </div>
13</form>
14
15public function fileUpload(Request $request) {
16    $this->validate($request, [
17        'input_img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
18    ]);
19
20    if ($request->hasFile('input_img')) {
21        $image = $request->file('input_img');
22        $name = time().'.'.$image->getClientOriginalExtension();
23        $destinationPath = public_path('/images');
24        $image->move($destinationPath, $name);
25        $this->save();
26
27        return back()->with('success','Image Upload successfully');
28    }
29}