To upload a file with validation in Laravel; create a form for file upload in blade view, and create routes and methods in the controller to handle file uploads in laravel 11 applications.
In this guide, we will create a form in laravel 11 with the help of which will be able to send the file to the controller, and validate it on the controller using “$request->validate([ 'file' => 'required|csv,txt,xls,xls,pdf|max:2048',])
” and store it in the folder with the help of “$request->file('file')->store('public/files')
“.
How to Upload a File in Laravel 11?
Here are some steps to upload a file with validation in laravel 11:
Step 1 – Set Up a New Laravel Application
To set up new laravel 11 application; just run the composer create-project --prefer-dist laravel/laravel myBlog
command on cmd or terminal window to do it:
composer create-project --prefer-dist laravel/laravel myBlog
Step 2 – Create Routes
Now to show the form for file upload and send the file to the controller, you have to create a route, which you can create on web.php, something like this:
use App\Http\Controllers\UploadController; Route::get('file-form', [UploadController::class, 'index']); Route::post('upload', [UploadController::class, 'store']);
Step 3 – Create Controller File
Run php artisan make:controller UploadController
command on cmd or terminal window to create a controller file:
php artisan make:controller UploadController
Now in this controller file, create 2 methods that will handle the file upload; Something like the following:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class UploadController extends Controller { public function index() { return view('file-upload'); } public function store(Request $request) { $validatedData = $request->validate([ 'file' => 'required|mimes:doc,docx,pdf,txt,json,xls|max:2048', ]); $path = $request->file('file')->store('public/files'); //write the logic here to store file in database return redirect('file-form')->with('status', 'File Has been uploaded successfully in Laravel'); } }
Step 4 – Create File Upload Form
Navigate to the resources/views folder, and create a file file-upload.blade.php
, and in this file, create a file upload form; as follows:
<!DOCTYPE html> <html> <head> <title>File Upload with Validation in Laravel 11 - Tutsmake.com</title> <meta name="csrf-token" content="{{ csrf_token() }}"> <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> </head> <body> <div class="container mt-4 "> <h2 class="text-center">File Upload with Validation in Laravel 11 - Tutsmake.com</h2> <form method="POST" enctype="multipart/form-data" id="upload" action="{{ url('upload') }}" > @csrf <div class="row"> <div class="col-md-6 offset-md-3"> <div class="form-group"> <input type="file" name="file" placeholder="Choose file" id="file"> @error('file') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> </div> <div class="col-md-6 offset-md-3"> <button type="submit" class="btn btn-primary" id="submit">Submit</button> </div> <br> <div class="col-md-6 offset-md-3"> @if(session('status')) <div class="alert alert-success"> {{ session('status') }} </div> @endif </div> </div> </form> </div> </div> </body> </html>
Step 5 – Start and Test Application
Run the php artisan serve
command to start the Laravel application and test it on the browser:
php artisan serve
Then open your browser and hit the following url on it:
http://127.0.0.1:8000/file-form