Image uploads with laravel

Laravel provides you built-in functions for uploading any type of file, Here is the code for you to upload an image in Laravel. This code is written in Laravel 5.4 version.

public function fileUpload(Request $request) {
    $this->validate($request, [
        'input_img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);
 
    if ($request->hasFile('input_img')) {
        $image = $request->file('input_img');
        $name = time().'.'.$image->getClientOriginalExtension();
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $name);
        $this->save();
 
        return back()->with('success','Image Upload successfully');
    }
}

 Use the controller file

use Illuminate\Support\Facades\Input;

Laravel 5.2  code 

This code is written in Laravel 5.2 version.

public function save(Request $request){
$destination = 'uploads/photos/'; // your upload folder
$image = $request->file('image');
$filename = $image->getClientOriginalName(); // get the filename
$image->move($destination, $filename); // move file to destination
// create a record
Student::create([
'image' => $destination . $filename
]);
return back()->withSuccess('Success.');
}

 Use the controller file

use Illuminate\Http\Request;