Upload Images in Laravel 6,Laravel 6 Images File Upload, Upload Images and Files with Validation in Laravel 6
Hello friends, today I will tell you through this tutorial how do you upload image files through Laravel 6. So we will learn step by step in Laravel 6.
Step 1: Create Route in Laravel 6
Step 2: Create Controller in Laravel 6
Step 3: Create Blade Files in Laravel 6
Step 1: Create Route in Laravel 6.0
Friends, this is the first step, in this step you have to give the url route path in the blade file in the routes file, as an example you are given below.
Route::get('image', function () { return view('image'); }); Route::post('fileupload', 'imageController@fileupload');
Step 2: Create Controller in Laravel 6.0
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
use Validator;
use Redirect;
use View;
class imageController extends Controller
{
public function fileupload(Request $request)
{
if($files=$request->file('file')){
$name=$files->getClientOriginalName();
$files->move('image',$name);
DB::table('images')->insert([
'file' => $name
]);
}
return redirect()->back()->with('message', 'Successfully Save Your Image file.');
}
}
Step 3: Create Blade Files in Laravel 6
resources/views/image.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 6.0 Image File Upload</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
</head>
<body>
<form action="{{url('fileupload')}}" enctype="multipart/form-data" method="post">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<div class="col-md-12">
<div class="form-group">
<label for="">File Select</label>
<input required type="file" class="form-control" name="file">
</div>
</div>
<div class="col-md-6">
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</form>
</body>
</html>