In this tutorial you will learn about the Laravel 7/6 File Upload Validation Example Tutorial and its application with practical example.
In this Laravel file Upload with validation example, we will learn how to upload and save file in laravel. In this laravel file upload example, I’ll show you how to validate upload file into folder and then save it into database. In this tutorial before saving file into database we will validate file and then save it into directory. Before uploading the files we will perform server side validation. After successfully file upload into the database and folder we will display success message on the screen.
Laravel 7/6 File Upload Validation Example Tutorial
- Install Laravel Fresh New Setup
- Setup Database Credentials
- Generate Migration & Model
- Add Route
- Create Controller & Methods
- Create Blade View
- Run Development Server
1). Install Laravel Fresh New Setup
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel Blog |
2). Setup Database Credentials
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
3). Generate Migration & Model
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model document -m |
The above command will create a model name file and also create a migration file for the file table. After successfully run the command go to database/migrations file and put the following code in it:
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 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatedocumentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('documents', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('documents'); } } |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
4). Add Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 |
Route::get('file', 'FileController@index'); Route::post('save', 'FileController@save'); |
5). Create Controller
Now, lets create a controller named FileController using command given below –
1 |
php artisan make:controller FileController |
After successfully create controller go to app/controllers/FileController.php and put the below code :
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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Validator,Redirect,Response,File; Use App\Document; class FileController extends Controller { public function index() { return view('file'); } public function save() { request()->validate([ 'file' => 'required|mimes:doc,docx,pdf,txt|max:2048', ]); if ($files = $request->file('fileUpload')) { $destinationPath = 'public/file/'; // upload path $profilefile = date('YmdHis') . "." . $files->getClientOriginalExtension(); $files->move($destinationPath, $profilefile); $insert['title'] = "$profilefile"; } $check = Document::insertGetId($insert); return Redirect::to("file") ->withSuccess('Great! file has been successfully uploaded.'); } } |
6). Create Blade view
In this step we will create a blade view file. Go to app/resources/views and create one file name file.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 |
<html lang="en" class=""> <head> <meta charset="UTF-8"> <title>Laravel File Upload Tutorial Example From Scratch</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" /> </head> <body> <div class="container"> <div class="row justify-content-center"> <div class="card"> <div class="card-header">Laravel Upload File Example</div> <div class="card-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.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="/save" method="post" enctype="multipart/form-data"> @csrf <div class="form-group"> <input type="file" class="form-control-file" name="file" id="file" aria-describedby="fileHelp"> <small id="fileHelp" class="form-text text-muted">Please upload a valid image file. Size of image should not be more than 2MB.</small> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </div> </div> </div> </html> |
7). 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 2 3 |
http://localhost:8000/file OR hit in browser http://localhost/blog/public/file |