Laravel 9 Multiple Files Download with Response Tutorial

Reading Time: 3 minutes
2,092 Views

Inside this article we will see the concept i.e Laravel 9 Multiple Files Download with Response. Article contains the classified information about downloading multiple files with laravel response. Laravel response provides download method which performs download function in laravel application.

If you are wondering to find a solution in laravel about file downloading with response then this article will help you a lot for this concept. You can use this concept to download Images, Files, PDFs, etc.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Laravel: Download Single File

Suppose we have an image file with name 1658717898.png inside /public/files folder.

To download this single image file, you have to write code as:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CustomController extends Controller
{
    public function index(Request $request)
    {
        return response()->download(public_path('files/1658717898.png'));
    }
}

But if you want to download multiple files then process will be different from this.

Laravel: Download Multiple Files

To download multiple files, you have to use ZipArchive concept. First you need to make zip of all files and then download it.

We will use the concept of File and ZipArchive of laravel 9. ZipArchive is a predefined class of laravel.

Here, is the complete code to make a zip file of /public/files folder and then download it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use File;
use ZipArchive;

class CustomController extends Controller
{
    public function downloadZip()
    {
        $zip = new ZipArchive;

        $fileName = 'my-images.zip';

        if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE) {

            $files = File::files(public_path('files'));

            foreach ($files as $key => $value) {
                $relativeNameInZipFile = basename($value);
                $zip->addFile($value, $relativeNameInZipFile);
            }

            $zip->close();
        }

        return response()->download(public_path($fileName));
    }
}

We hope this article helped you to Learn Laravel 9 Multiple Files Download with Response Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.