How To Create Zip File and Download in Laravel 8

Reading Time: 3 minutes
4,895 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

Run this command into project terminal 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.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more