How To Create Zip File and Download in Laravel 8

Share this Article
Reading Time: 3 minutes
2,738 Views

Inside this article we will see the concept to create zip file and download in laravel 8. Article contains classified information about creating zip file and download it.

If you are looking for an article which gives you understanding about create zip file and download in laravel 8 then you are at right place to learn and implement it. This is step by step tutorial to create a zip file of images or we can understand collection of files etc.

We will use the concept of File and ZipArchive in laravel 8.

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Collection of Files to Create Zip

Suppose we have a folder with name images in /public folder and we want to create a zip file of it and download via browser.

Inside images folder we have some images.

Next,

We need to write logic to create zip file of images folder.


Create Controller

Open project into terminal and run this artisan command to create controller file.

$ php artisan make:controller ZipController

It will creates ZipController.php inside /app/Http/Controllers folder.

This controller contains all the logic to create zip file and download that.

Open ZipController.php and write this complete code into it.

<?php

namespace App\Http\Controllers;

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

class ZipController 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('images'));

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

            $zip->close();
        }

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

Add Routes

Open web.php from /routes folder and add this route into it.

//...

use App\Http\Controllers\ZipController;

Route::get('download-zip', [ZipController::class, "downloadZip"]);

//...

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

URL: http://127.0.0.1:8000/download-zip

When we hit this URL, it downloads my-images.zip file into your system

We hope this article helped you to learn How To Create Zip File and Download in Laravel 8 Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5