• May 3, 2024
  • Chiwetara Igwe
  • 0

In this tutorial, we will create two routes: the first one will be a GET method to render form that will be used for uploading our file and and the second will be a POST method to process the uploaded files. We created a simple form with a file input. So, you have to simply select a file and then it will upload in the “uploads” directory of the public folder.

So, you have to simply follow the steps below and get the file upload in Laravel 11 application.

Step 1: Install Laravel

This step is not required; however, if you have not created a Laravel project, then you may go ahead and execute the below command:

composer create-project Laravel/Laravel sample-project

Step 2: Create Controller

In this step, we will create a new Controller named MultipleFilesUpload. In this file, we will add two methods: upload() and processFiles() to render views and handle the file upload logic.

Let’s create the MultipleFilesUpload by following this command:

php artisan make: controller MultipleFilesUpload

Next, let’s update the following code to the Controller file.

app/Http/Controllers/MultipleFilesUpload.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MultipleFilesUpload extends Controller
{

    public function upload()
    {
        return view('upload-multiple-file-upload');
    }

    public function processFiles(Request $request)
    {

        foreach ($request->product_images as $file) {

            $filename = $file->getClientOriginalName();

            $file->storeAs('uploads', $filename, 'public');

        }

    }
}

Step 3: Create Routes

Furthermore, open `routes/web.php` file and add the routes to manage GET and POST requests for rendering views and storing file logic.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MultipleFilesUpload;
  
Route::get('upload-multiple-files', [MultipleFilesUpload::class, 'upload']);
Route::post('process-multiple-files', [MultipleFilesUpload::class, 'processFiles'])->name('processFiles');

Step 4: Create Blade File

At the last step, we need to create a `fileUpload.blade.php` file. In this file, we will create a form with a file input button. So, copy below and paste it into that file.

resources/views/upload-multiple-file-upload.blade.php

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel Multiple Files Upload</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>

<body>
    <div class="container mt-4">
        <div class="card mt-5">
            <h3 class="card-header p-3">Laravel Multiple Files Upload</h3>
            <div class="card-body">
                <form action="{{ route('processFiles') }}" method="post" enctype="multipart/form-data">
                    @csrf
                    <div class="mb-3">
                        <label for="formFile" class="form-label">Product Images</label>
                        <input class="form-control" name="product_images[]" multiple type="file" id="formFile">
                    </div>

                    <div class="col-12">
                        <button class="btn btn-primary" type="submit">Submit form</button>
                    </div>

                </form>
            </div>
        </div>
    </div>

</body>

</html>

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/upload-multiple-files

Chiwetara Igwe

I'm a full-stack developer, entrepreneur and owner of techbly.ng. I live in Nigeria and I love to write tutorials and tips that can help to other web developers. I am experienced in PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap. I am passionate about learning new things and keeping up with the latest trends and technologies.

http://techbly.ng

Leave a Reply

Your email address will not be published. Required fields are marked *

× Have a question?