How to File Upload in Laravel 11 Example

Today, I'm excited to walk you through a simple example of how to upload files in Laravel 11. File uploading is a common task in web development, and Laravel makes it incredibly easy with its powerful features.

In this tutorial, we'll cover the basics of handling file uploads, including how to create the necessary routes, controllers, and views. Also, see how to validate files in laravel 11 and how to check file size in laravel 11.

So, let's see the laravel 11 file upload, laravel 11 file type validation, and laravel 11 file size validation.


Step-by-step Guide: Upload File in Laravel 11

Step 1: Install Laravel 11

To begin, we'll install Laravel using the following Composer command.

composer create-project laravel/laravel laravel-11-example


Step 2: Creating Controller

Next, we'll generate the FileController. Within this controller, we'll define two functions. To do so, please execute the following command:

php artisan make:controller FileController


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
class FileController extends Controller
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function index(): View
        return view('file-upload');
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function store(Request $request): RedirectResponse
            'file' => 'required|mimes:pdf,xlx,csv|max:4096',
        $file_name = time().'.'.$request->file->extension();  
        $request->file->move(public_path('uploads'), $file_name);
            Write Code Here for
            Store $file_name name in DATABASE from HERE 
        return back()->with('success', 'File uploaded successfully!')
                     ->with('file', $file_name);

Note: create an uploads folder in the public folder.

Store File in Public Folder

$request->file->move(public_path('uploads'), $file_name);
// public/uploads/test.csv

Store File in Storage Folder

$request->file->storeAs('uploads', $file_name);
// storage/app/uploads/test.csv

Store File in S3

$request->file->storeAs('uploads', $file_name, 's3');
Step 3: Creating Routes

Next, we'll define routes for displaying and storing images in Laravel 11. To do this, open the web.php file.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FileController;
Route::get('file-upload', [FileController::class, 'index']);
Route::post('file-upload', [FileController::class, 'store'])->name('');


Step 4: Creating Blade File

Create a new Blade file named file-upload.blade.php. This file will contain the HTML markup for the file upload form.


<!DOCTYPE html>
    <title>How to File Upload in Laravel 11 Example - Techsolutionstuff</title>
    <link href="" rel="stylesheet" crossorigin="anonymous">
    <link rel="stylesheet" href="" />
<div class="container">
    <div class="card mt-5">
        <h3 class="card-header p-3"><i class="fa fa-star"></i> How to File Upload in Laravel 11 Example - Techsolutionstuff</h3>
        <div class="card-body">
                <div class="alert alert-success" role="alert"> 
                    {{ $value }}
            <form action="{{ route('') }}" method="POST" enctype="multipart/form-data">
                <div class="mb-3">
                    <label class="form-label" for="input-file">File:</label>
                        class="form-control @error('file') is-invalid @enderror">
                        <span class="text-danger">{{ $message }}</span>
                <div class="mb-3">
                    <button type="submit" class="btn btn-success"><i class="fa fa-save"></i> Upload</button>


Step 5: Run the Laravel 11 Application

In the last step, execute the following command to start the Laravel development server.

php artisan serve


You might also like:


Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.