Laravel 9 File Upload Example

Websolutionstuff | Mar-11-2022 | Categories : Laravel

In this artical, we will explain the laravel 9 file upload example step by step. As we know file upload is the most common task in web development. So, here I will show you how to upload file in laravel 9. In this example, you will learn how to upload file in laravel 9 into the database and storage directory with validation.

Also,we will see how to validate file mime type, size, etc using laravel 9 validation rules

So, let's see file upload in laravel 9 or file upload example in laravel 9

 

Step 1 : Add Routes

First of all, you need to install laravel if you have already then add routes in the routes/web.php file shown below.

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FileUploadController;

Route::get('file-upload', [FileUploadController::class, 'index']);
Route::post('file-upload', [FileUploadController::class, 'store'])->name('file.store');

 

 

Step 2: Create FileUploadController

Now, create new FileUploadController add two method index() and store(). here the first method will handle get method another one for post. So, copy the below code.

app/Http/Controllers/FileUploadController.php

<?php
  
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
  
class FileUploadController extends Controller
{
    public function index()
    {
        return view('fileUpload');
    }

    public function store(Request $request)
    {
        $request->validate([
            'file' => 'required|mimes:pdf,xlx,csv|max:1024',
        ]);
    
        $fileName = time().'.'.$request->file->extension();  
     
        $request->file->move(public_path('uploads'), $fileName);
   
     
        return back()
            ->with('success','File Uploaded successfully.')
            ->with('file', $fileName);
   
    }
}

 

 

Store File in Public Folder

$request->file->storeAs('uploads', $fileName); 

// storage/app/uploads/filename.jpg

 

Store File in S3

$request->file->storeAs('uploads', $fileName, 's3');

 

Store File in Storage Folder

$request->file->move(public_path('uploads'), $fileName);

// public/uploads/filename.jpg

 

Step 3: Create Blade File

In the blade file, we will create a basic form with an upload button. So, add the below code.

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 File Upload Example - websolutionstuff.com</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
       
    <div class="panel panel-primary">
  
        <div class="panel-heading text-center mt-5">
            <h2>Laravel 9 File Upload Example - websolutionstuff.com</h2>
        </div>
  
        <div class="panel-body mt-5">
       
            @if ($message = Session::get('success'))
                <div class="alert alert-success alert-dismissible fade show mb-2" role="alert">
                    {{ $message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif
      
            <form action="{{ route('file.store') }}" method="POST" enctype="multipart/form-data">
                @csrf
      
                <div class="mb-3">
                    <label class="form-label" for="inputFile">Select 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">Upload</button>
                </div>
           
            </form>
      
        </div>
    </div>
</div>
</body>
</html>

 

 


You might also like :

Recommended Post
Featured Post
How To Integrate Razorpay Payment Gateway In Laravel 9
How To Integrate Razorpay Paym...

In this article, we see how to integrate razorpay payment gateway in laravel 9. As you all know if you are developi...

Read More

Apr-11-2022

Copy To Clipboard JQuery
Copy To Clipboard JQuery

In this article, we will see how to copy to clipboard jQuery. we will learn how to copy text from textarea&nbs...

Read More

Aug-19-2020

How to Get Selected Checkbox Value in Array Using jQuery
How to Get Selected Checkbox V...

In this post we will see how to get selected checkbox value in array using jquery. Here i will give you some example to&...

Read More

May-24-2021

How To Send Email In Laravel 9 Using Mailgun
How To Send Email In Laravel 9...

In this article, how to send email in laravel 9 using mailgun. we will learn laravel 9 to send emails using mailgun...

Read More

Jul-29-2022