In this article, we will see how to create a zip file using zipArchive in laravel. Sometimes we have requirements to have functionalities like creating zip files for documentation or images and downloading them. So, at that time we can find many laravel packages to perform this task. But here, we will see how to create a zip file in laravel using zipArchive without any package.
Laravel provides ZipArchive class for creating zip files in laravel. So, we will use ZipArchive in laravel 6 and laravel 7, and laravel 8.
In the below code, I have created one function in the laravel controller and added ZipArchive class.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use ZipArchive;
public function ZipArchiveExample()
{
$zip = new ZipArchive;
$fileName = 'Example.zip';
if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
{
$files = \File::files(public_path('Zip_Example'));
foreach ($files as $key => $value) {
$file = basename($value);
$zip->addFile($value, $file);
}
$zip->close();
}
return response()->download(public_path($fileName));
}
You might also like:
In this article, we will see cron job scheduling in laravel. Many times we require to run some piece of code in a specif...
Sep-28-2020
In this tutorial, we will see how to disable future dates in jquery datepicker. In the date picker, today's dat...
Jun-17-2022
In this article, we will see how to image upload in CKEditor with laravel 10. Here, we will learn about image uploa...
May-08-2023
In this article, we will see the laravel 9 vue js crud operation example. Here, we will learn how to create a vue 3...
Dec-07-2022