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.

laravel_11_file_upload

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

app/Http/Controllers/FileController.php

<?php
  
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
    {
        $request->validate([
            '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.

routes/web.php

<?php
  
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('file.store');

 

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.

resources/views/file-upload.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How to File Upload in Laravel 11 Example - Techsolutionstuff</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdThisnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" />
</head>
        
<body>
<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">
  
            @session('success')
                <div class="alert alert-success" role="alert"> 
                    {{ $value }}
                </div>
            @endsession
            
            <form action="{{ route('file.store') }}" method="POST" enctype="multipart/form-data">
                @csrf
        
                <div class="mb-3">
                    <label class="form-label" for="input-file">File:</label>
                    <input 
                        type="file" 
                        name="file" 
                        id="input-file"
                        class="form-control @error('file') is-invalid @enderror">
      
                    @error('file')
                        <span class="text-danger">{{ $message }}</span>
                    @enderror
                </div>
         
                <div class="mb-3">
                    <button type="submit" class="btn btn-success"><i class="fa fa-save"></i> Upload</button>
                </div>
             
            </form>
        </div>
    </div>
</div>
</body>
      
</html>

 

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

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. 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.

RECOMMENDED POSTS

FEATURE POSTS