Laravel 5.5 create zip archive file and download example


Are you want to create zip file programmatically in laravel application then chumper/zipper composer package is help to generate zip file simply. Sometime we need to create zip file with images or other files, then also you have to give download as response.

In this article i will let you know how to generate zip file and download as response in laravel 5.5 application using chumper/zipper composer package. you have to just follow few step to generate zip file in laravel 5.5 application.

Install chumper/zipper Package:

first thing is to install composer package in our project. so install chumper/zipper composer package by following composer command in your laravel 5.5 application.

composer require chumper/zipper

After successfully install package, open config/app.php file and add service provider and alias.

config/app.php

'providers' => [

....

'Chumper\Zipper\ZipperServiceProvider'

],

'aliases' => [

....

'Zipper' => 'Chumper\Zipper\Zipper'

]

Add Route:

Now we will add route for demo example, so simply add following route in your route file, here we will add "downloadZip" route and you can download created zip file:

routes/web.php

Route::get('downloadZip', 'HomeController@downloadZip');

Create HomeController method:

Here, we require to create new controller HomeController that will manage index method of route. So let's put bellow code.

HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

function downloadZip(){

$files = glob(public_path('css/*'));

\Zipper::make(public_path('test.zip'))->add($files)->close();


return response()->download(public_path('test.zip'));

}

}

You can simply run this example and check it.

I hope you found your best.


August 28, 2017 | 903 Viewer | Category : PHP Laravel

Related Posts: