Laravel 11 File Upload Example

Laravel 11 File Upload Example

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:


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>
  <title>File Upload with Validation in Laravel 11 -</title>
  <meta name="csrf-token" content="{{ csrf_token() }}">
 <link href="[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<div class="container mt-4 ">

  <h2 class="text-center">File Upload with Validation in Laravel 11 -</h2>

      <form method="POST" enctype="multipart/form-data" id="upload" action="{{ url('upload') }}" >
          <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">
                        <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
              <div class="col-md-6 offset-md-3">
                  <button type="submit" class="btn btn-primary" id="submit">Submit</button>
              <div class="col-md-6 offset-md-3">
                              <div class="alert alert-success">
                                  {{ session('status') }}



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:

Recommended Guides


Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *