File Upload Example In Laravel 11

  Mar 2024
  ITSolutionsGuides
  Category: Laravel
File Upload Example In Laravel 11

Welcome To ITSolutionsGuides,


In this Laravel 11 file upload example, we'll explore how to seamlessly handle file uploads within your Laravel 11 application. In Laravel 11, file uploads is crucial for modern web applications where user will upload the file through the input field. When the file is uploaded initialy we should validate the file uploaded and then we should move the uploaded file to new directory and then save the path of the directory with the file name in the database

Let's Create Project & Controller

Lets create the new laravel project using the command,
laravel new file_upload

After creating the project lets create the controller for the image upload using the following artisan command, for detailed explanation about creating the controller

php artisan make:controller FileController


app/Http/Controllers/FileController.php

<?php
  
namespace App\Http\Controllers;
       
use Illuminate\Http\Request;
use Illuminate\View\View;
use App\Models\Document;
use Illuminate\Http\RedirectResponse;
      
class FileController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(): View
    {
        return view('fileUpload');
    }
        
    /**
     * 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:2048',
        ]);
        
        $fileName = time().'.'.$request->file->extension();  
         
        $request->file->move(public_path('uploads'), $fileName);
       
        Document::create(['filename' => $fileName ]);
         
        return back()->with('success', 'File uploaded successfully!')
                     ->with('file', $fileName);
   
    }
}

Let's Create Routes

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');

Let's Create Blade Files

Lets create the blade file , create website using artisan command



resources/views/fileUpload.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>File Upload Example In Laravel 11 - ITSolutionsGuides</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> File Upload Example In Laravel 11 - ITSolutionsGuides</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="inputFile">File:</label>
                    <input 
                        type="file" 
                        name="file" 
                        id="inputFile"
                        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>

Let's Run The Project

Lets run the laravel project using the following artisan command,

php artisan serve

enter the below url in the browser,

http://localhost:8000/file-upload

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides