Inside this article we will see the concept to create zip file and download in laravel 9. 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 9 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 9. ZipArchive is a predefined class of laravel.
Learn More –
- Laravel 9 Database Seeding from JSON File Tutorial
- Laravel 9 Export MySQL Table Data into CSV File Tutorial
- Laravel 9 File or Image Upload Using Livewire Tutorial
- Laravel 9 FullCalendar Ajax CRUD Tutorial Example
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
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 create 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 9 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.