Laravel 11 File Upload Validation

Laravel 11 provides a comprehensive to upload file with validation feature. You can upload file with validation for different perspective, such as file types, file size etc. This article give you a-z guidelines to upload file along with validation from laravel 11 framework

  1. Here we will follow some steps. Those are 
  2. Install Laravel 11 Fresh Project
  3. Install and Create Bootstrap 5 Blade
  4. Create Necessary Routes
  5. Create Necessary Controller and Methods

Install Laravel 11 Fresh Project

Before starting work we need to install laravel 11 project. Using below command a fresh laravel 11 project will be installed in system

composer create-project --prefer-dist laravel/laravel laravel11-fileupload

Create Routes for Accepting and Forwarding Request

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FileUploadController;

Route::get('/upload', [FileUploadController::class, 'showForm'])->name('upload.form');
Route::post('/upload', [FileUploadController::class, 'uploadFile'])->name('upload.file');

Create FileUploadController Class

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FileUploadController extends Controller
{
    public function showForm()
    {
        return view('upload');
    }

    public function uploadFile(Request $request)
    {
        $request->validate([
            'file' => 'required|mimes:pdf,doc,docx,xls,xlsx|max:5120', // Max file size: 5MB and allow pdf,doc,docx,xls,xlsx file
]); if ($request->file('file')) { $file = $request->file('file'); $fileName = time() . '_' . $file->getClientOriginalName(); $file->storeAs('uploads', $fileName, 'public'); // Save to public/uploads return back()->with('success', 'File uploaded successfully!')->with('file', $fileName); } return back()->with('error', 'Please select a file.'); } }

Create Blade File Upload.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laravel File Upload</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="d-flex justify-content-center align-items-center vh-100 bg-light">

<div class="card p-4 shadow-sm" style="width: 600px;">
    <h2 class="h5 mb-3 text-center">File Upload Validation</h2>

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
        <p>Uploaded File: <a href="{{ asset('uploads/' . session('file')) }}" target="_blank">{{ session('file') }}</a>
        </p>
    @endif

    @if(session('error'))
        <div class="alert alert-danger">{{ session('error') }}</div>
    @endif

    <form action="{{ route('upload.file') }}" method="POST" enctype="multipart/form-data">
        @csrf
        <div class="mb-3">
            <label for="file" class="form-label">Choose a file</label>
            <input class="form-control" type="file" id="file" name="file" required>
            @error('file')
            <small class="text-danger">{{ $message }}</small>
            @enderror
        </div>
        <button type="submit" class="btn btn-primary w-100">Upload</button>
    </form>
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Read also: Laravel export collection to xlsx file download

Read also: Make sitemap using laravel

laravel 11 file upload validation example - programmingmindset.com

Run Project and Upload File

php artisan serve

This will start and serve project on http://127.0.0.1:8000; After that visit route /upload and try to upload image file. This will show file validation error. As we have added public directory uploads, so file will be uploaded inside storage/app/public/uploads directory. You can then create storage:link by running command php artisan storage:link

laravel 11 file upload validation - programmingmindset.com

Read also: Laravel 11 file upload in public directory

Read also: Laravel 11 file upload in storage directory

Read also: Host multiple laravel project on same port in nginx server

Tags: