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.
Do you like below Tutorials ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example