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 :
- Attributes Set In Laravel Model
- Laravel 10 Http Guzzle Request Crud Example
- How to return laravel collection in json format - toJson() Method - Laravel Collection
- Trying to access array offset on value of type int
- Where Clause In Laravel Eloquent Example
- Everything know about Laravel Localization With Demo Example
- How to setup Google's Two Factor Authentication in Laravel
- How to send mail example in Laravel 6
- Laravel 10 Firebase Web Push Notification Tutorial
- Laravel 10 Create Controller Example