In this tutorial I will give you example of how to create zip file in laravel 7/8. Some times client's have requirments to have functionalities like create zip file for documantation or images and download it. So, using ziparchive function you can create zip file and download in laravel 7/8.
In this example I will show you to how to create zip file in laravel using ziparchive without any packege. Laravel provide ZipArchive class for create zip file in laravel,So I will use ZipArchive in laravel 7/8.
Read More Official Document of PHP : ZipArchive
In below code I have created one function in laravel controller and added ZipArchive class.
In this step we can add route for create and download zipfile. So, add below code in web.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ZipFileController ;
Route::get('ziparchive_example', [ZipFileController ::class, 'ZipArchiveExample']);
Now, create controller and add function ZipArchiveExample.
app/Http/Controllers/ZipFileController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use ZipArchive;
class ZipFileController extends Controller
{
public function ZipArchiveExample()
{
$zip = new ZipArchive;
$fileName = 'Zipfile_Example.zip';
if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
{
$files = \File::files(public_path('ZipArchive_Example'));
foreach ($files as $key => $value) {
$file = basename($value);
$zip->addFile($value, $file);
}
$zip->close();
}
return response()->download(public_path($fileName));
}
}
Now run below command in your terminal.
Now you can open bellow URL on your browser:
You might also like :
In this small post i will show you how to upload file on the ftp server using php. As we know there are many ftp functio...
May-20-2021
In this article, we will see how to toggle between dark and light modes using jquery. As per the current trend of web de...
Nov-24-2020
In this tutorial, I will explain you to how to get the selected checkbox value from a checkbox list in jquery, If y...
Jun-17-2020
In this tutorial, I will guide you through the process of adding a date range picker component to your Angular 15 applic...
Jun-30-2023