Laravel Validation File Example




Laravel Validation File Example

Hi guys, Today i will explained to the Laravel Validation file Example in your laravel project.Creating a feature to upload a file is a common task for any web developer; millions of photos, videos and documents daily updated to various websites.Laravel Validation file Example is so easy to you can just follow my step by step and learn Laravel Validation file Example.

So let's start to the example and follow to the my all step.



'file' => 'required|mimes:png,jpg,jpeg,csv,txt,xlx,xls,pdf|max:2048'


Example :

Step 1: Create Route

Last step to create a route in web.php file and use this code.

Route :routes/web.php


use Illuminate\Support\Facades\Route;

use App\Http\Controllers\UploadController;



| 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('upload/file', [UploadController::class, 'index']);

Route::post('upload/file/data', [UploadController::class, 'uploadFile'])->name('upload.file');

Step 2: Create a UploadController Controller

Next you can require to the UploadController so create a UploadController in just following command through.

Controller : app/Http/Controllers/UploadController.php


namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

use Validator;

class UploadController extends Controller



* The attributes that are mass assignable.


* @var array


public function index(){

return view('uploadFile');



* The attributes that are mass assignable.


* @var array


public function uploadFile(Request $request){


'file' => 'required|mimes:png,jpg,jpeg,csv,txt,xlx,xls,pdf|max:2048'


$file = new File;

if($request->file()) {

$name = time().'_'.$request->file->getClientOriginalName();

$filePath = $request->file('file')->storeAs('uploads', $name, 'public');

$file->name = time().'_'.$request->file->getClientOriginalName();

$file->file = '/storage/' . $filePath;


return back()

->with('success','File has uploaded to the database.')

->with('file', $name);




Step 4: Create a UploadFile Blade File

Next you can require to the uploadFile.blade.php so create a uploadFile.blade.php in just following step.

View :resources/views/uploadFile.blade.php

<!DOCTYPE html>

<html lang="en">


<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<link rel="stylesheet" href="">

<title>Laravel Validation File Example</title>


.container {

max-width: 450px;


dl, ol, ul {

margin: 0;

padding: 0;

list-style: none;





<div class="container mt-5">

<div class="card" style="width: 25rem;">

<h5 class="card-title text-center mt-3">Laravel Validation File Example</h5>

<div class="card-body">

<form action="{{route('upload.file')}}" method="post" enctype="multipart/form-data">


@if ($msg = Session::get('success'))

<div class="alert alert-success">

<strong>{{ $msg }}</strong>



@if (count($errors) > 0)

<div class="alert alert-danger">


@foreach ($errors->all() as $error)

<li>{{ $error }}</li>





<div class="custom-file">

<input type="file" name="file" class="custom-file-input" id="chooseFile">

<label class="custom-file-label" for="chooseFile">Choose file</label>


<button type="submit" name="submit" class="btn btn-primary btn-block mt-4">

Upload Files








So, finally we are done with our code we can get below output.

php artisan serve

Browser url run : http://localhost:8000/upload/file

#Laravel 8

#Laravel 7


#Laravel 6