How to Create Zip File and Download in Laravel 6?

How to Create Zip File and Download in Laravel 6?

In this tutorial, i am writing example of Laravel 6 create zip archive file and download in response. I will give you example step by step how to create zip file from folder and download in Laravel 6. We will create zip file using ziparchive class in PHP Laravel 6 Application.

In this post, I will show you how to create very simple way to zip file in Laravel 6 application. So let's follow few things and make it simple example.

Step 1: Create Route

First thing is we put one route in one for download created zip file. So simple add both routes in your route file.

routes/web.php

Route::get('download-zip', '[email protected]');

Step 2: Create Controller

Same things as above for route, here we will add one new method for route. downloadZip() will generate new zip file and download as response, so let's add bellow:

app/Http/Controllers/ZipController.php

<?php



namespace App\Http\Controllers;



use Illuminate\Http\Request;

use File;

use ZipArchive;



class ZipController extends Controller

{

    /**

     * Display a listing of the resource.

     *

     * @return \Illuminate\Http\Response

     */

    public function downloadZip()

    {

        $zip = new ZipArchive;



        $fileName = 'myNewFile.zip';



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

        {

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



            foreach ($files as $key => $value) {

                $relativeNameInZipFile = basename($value);

                $zip->addFile($value, $relativeNameInZipFile);

            }



            $zip->close();

        }



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

    }

}

Ok now you can run project and open that route like.

But make sure you have "myFiles" folder in public directory and add some pdf files on that file so it will create zip file with those files.

Now you can test it by using following command:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/download-zip

I hope it can help you...

laravel php web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...