Watch Youtube Video (HINDI) :
How to Create Zip File and Download in Laravel, In this comprehensive example we will learn how to create a zip with a bunch of images in a single zip file and a Zip archive with download.
How to Create Zip Files in Laravel, If you are finding the best way to handle zip file Zip archive and create a zip file in laravel this post definitely will help you to create it in your application.
To create a Zip file and download it I am creating a ZipController for a better understanding of you how to Zip archive in the laravel application best way.
Laravel Adding Social Media Share buttons Advanced Tutorial Example
Create A ZipController
Using the below-provided command, create a ZipController in your laravel application.
php artisan make:controller ZipController
Create Route
Create a route with the GET method for the run controller method. The route will run the ZipController method for making a zip file from the images folder inside the public folder.
routes/web.php<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\ZipController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('make-zip',[ZipController::class,'downloadZip']);
ZipController
Define a routing method here download zip, import File, and ZipArchive in ZipController. and follow the given example in your application you will generate a zip file using an images folder with all images in a single zip file and it will also ask to download the zip file.
app/Http/Controllers/ZipController .php<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use File; use ZipArchive; class ZipController extends Controller { public function downloadZip() { $zip = new ZipArchive; $zipFileName = time().'.zip'; if ($zip->open(public_path($zipFileName),ZipArchive::CREATE) === true) { $images = File::files(public_path('images')); foreach ($images as $key => $value) { $baseName = basename($value); $zip->addFile($value,$baseName); } $zip->close(); } return response()->download(public_path($zipFileName)); } }
Start Development Server
Start your application development server with the suggested command in your terminal.
php artisan serve
Open your browser, and run the below-given URL in your browser.
http://127.0.0.1:8000/make-zip
How To Generate Image From HTML Using Snappy In Laravel 9
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Dynamic barcode generate in laravel example
- How to Integrate Razorpay Payment Gateway in Laravel 9 Example
- Route Resource Controller Laravel
- Laravel 6 Image Upload Tutorial
- How To Add After Validation Hook In Laravel Example
- How to Install Laravel 10 With Video Tutorial
- How To Validate Youtube Url In laravel Best Practical Example
- All Know About Different Root Path Of The Laravel Application With Example
- Laravel 9 Seeder and How to Seeding
- Laravel 10 Restful API Passport Authentication Tutorial