In this tutorial you will learn about the Laravel 8 File Upload Example Tutorial and its application with practical example.
In this Laravel 8 file upload example tutorial, I’ll show you how to upload files in laravel 8 application with validation. I will also show you how to validate upload file into folder and then save it into database. In this tutorial you will learn how to upload file in laravel. In this article I will share example to upload file in laravel application. In this example before saving file into database we will validate file and then save it into directory using.
Laravel 8 File Upload Example Tutorial
In this step by step tutorial I’ll demonstrate how to upload file in laravel 8 with validation. Please follow the steps given below:
Step 1 : Install Laravel 8
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Create Routes
Now, we need to add following routes in “routes/web.php” file along with a resource route. Lets open “routes/web.php” file and add following route.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\FileUploadController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('file-upload', [FileUploadController::class, 'fileUpload'])->name('file.upload'); Route::post('file-upload', [FileUploadController::class, 'fileUploadPost'])->name('file.upload.post'); |
Step 3: Create File Upload Controller
Now, lets create a controller for simple file uploading. Create a controller named FileUploadController using command given below –
1 |
php artisan make:controller FileUploadController |
Once the above command executed, it will create a controller file FileUploadController.php in app/Http/Controllers/ directory. Open the FileUploadController.php file and put the following code in it.
app/Http/Controllers/FileUploadController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class FileUploadController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function fileUpload() { return view('fileUpload'); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function fileUploadPost(Request $request) { $request->validate([ 'file' => 'required|mimes:pdf,xlx,csv|max:2048', ]); $fileName = time().'.'.$request->file->extension(); $request->file->move(public_path('uploads'), $fileName); return back() ->with('success','You have successfully upload file.') ->with('file',$fileName); } } |
Step 3: Create Blade File
In this step, we will create view/blade file to generate and display file Upload Form. Lets create a blade file “fileUpload.blade.php” in “resources/views/” directory and put the following code in it respectively.
resources/views/fileUpload.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<!DOCTYPE html> <html> <head> <title>laravel 8 file upload example</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container"> <div class="panel panel-primary"> <div class="panel-heading"><h2>laravel 8 file upload example</h2></div> <div class="panel-body"> @if ($message = Session::get('success')) <div class="alert alert-success alert-block"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>{{ $message }}</strong> </div> @endif @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input. <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ route('file.upload.post') }}" method="POST" enctype="multipart/form-data"> @csrf <div class="row"> <div class="col-md-6"> <input type="file" name="file" class="form-control"> </div> <div class="col-md-6"> <button type="submit" class="btn btn-success">Upload</button> </div> </div> </form> </div> </div> </div> </body> </html> |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/file-upload |