How To Create Zip File and Download in Laravel 8

Share this Article
Reading Time: 3 minutes
776 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.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at 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 these routes 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

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.

Learn Web Development Courses @ $4 only.
Web Development Courses @ $4