How To File Upload In Laravel 9 Example

In this article, we will see how to file upload in laravel 9 example. Here we will show you the laravel 9 file upload in the pubic folder example. In this example, you will learn how to upload files in laravel 9 into the database and storage directory with validation. Also, we will see how to validate file mime type, file size, etc using laravel 9 validation rules.

For the file upload example, we will add two routes and create functions in the controller for the validation of the file. Also, we will create a form to submit files.

So, let's see a file upload in laravel 9.

Step 1: Add Routes

In this step, we will install laravel if you have already then add routes in the web.php file.

routes/web.php

<?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 a new FileUploadController and add two methods index() and store().

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

We will create a basic form with an upload button in the blade file.

<!DOCTYPE html>
<html>
<head>
    <title>How To File Upload In Laravel 9 Example - Techsolutionstuff</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/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>How To File Upload In Laravel 9 Example - Techsolutionstuff</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:

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