How to Upload File in Laravel 8 with Validation

In this tutorial you will learn about the How to Upload File in Laravel 8 with Validation and its application with practical example.

In this Laravel file Upload with validation example, we will learn how to upload and validate file before upload in laravel. In this laravel file upload example, I’ll show you how to validate and 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.

How to Upload File in Laravel 8 with Validation

  • Step 1 – Download Laravel 8 Application
  • Step 2 – Database Configuration
  • Step 3 – Build File Model & Migration
  • Step 4 – Create Routes
  • Step 5 – Build Upload Controller By Artisan Command
  • Step 6 – Create File Upload Form
  • Step 7 – Create Directory inside Storage/app/public
  • Step 8 – Run Development Server

Step 1 – Download Laravel 8 Application

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

Step 2 – Database Configuration

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.

Step 3 – Build File Model & Migration

Now, in this step we will create model and migration file. Please run the following command:

After that, open create_files_table.php file inside /database/migrations/ directory. And put the following code in it:

Now, in this step we will create model and migration file. Please run the following command:

Step 4 – Create Routes

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

Step 5 – Build File Upload Controller By Artisan Command

Now, lets create a controller named FileUploadController using command given below –

After that, go to app/http/controllers and open FileUploadController.php file. And put  the following code into it:

The following line of code will upload an file into the files directory:

Step 6 – Create File Upload Form

Go to resources/views and create file-upload.blade.php and update the following code into it:

The following below code will display the validation error message on the blade view file:

Step 7 – Create Directory inside Storage/app/public

Now, create directory name files inside storage/app/public directory. Because the following line of code will upload an file into the files directory, which is located inside storage/app/public/ directory:

Step 8 – Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

In this tutorial we have learn about the How to Upload File in Laravel 8 with Validation and its application with practical example. I hope you will like this tutorial.