Learn how to generate and download a zip file in Laravel using the ZipArchive class. Follow this step-by-step guide to compress and download files seamlessly.
In this guide, we’ll explore how to generate and download a zip file in Laravel using the ZipArchive class. This powerful PHP feature allows you to easily create and download zip files in your Laravel application. We’ll walk through the process step-by-step to help you implement this functionality in your own projects.
By the end of this guide, you’ll know exactly how to create a zip file in Laravel using ZipArchive and download it with just a few lines of code.
Step 1: Create a Route
To begin, we’ll first define a route in the web.php file that will point to our controller method.
Add the following code to your routes/web.php file:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ZipArchiveController;
Route::get('download-zip', [ZipArchiveController::class, 'downloadZip']);
This route triggers the downloadZip method in our ZipArchiveController, which handles the creation and downloading of the zip file.
Step 2: Create the Controller
Next, create a new controller called ZipArchiveController. This controller will contain the logic to create the zip file and make it available for download.
Here’s the code for the controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use ZipArchive;
class ZipArchiveController extends Controller
{
public function downloadZip()
{
$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));
}
}
In this code:
- We use the ZipArchive class to create a zip file.
- The $files array contains files from the Zip_Example folder (which you need to create in the public directory).
- The downloadZip() method creates the zip file, adds the files from the folder, and makes it available for download.
Step 3: Set Up the Folder Structure
You need to create a folder named Zip_Example in your public directory. This folder will contain the files you want to zip. Once the folder is set up, the controller will add these files to the zip file.
Access the Download Route
You can now access the download functionality by visiting the following URL in your Laravel 9 application:
http://localhost:8000/download-zip
For more detail you can write Laravel Documentation
Summary
By following these steps, you’ve successfully implemented functionality to generate and download zip files in Laravel 9 using the ZipArchive class. This feature can be particularly useful when dealing with large sets of files or when you want to compress and distribute files to your users.
You may also find interesting:
Sync Google Calendar with Your Laravel Site: A Comprehensive Guide
[…] Laravel Create Zip File and Download Example: A Comprehensive Guide […]
[…] Laravel Create Zip File and Download Example: A Comprehensive Guide […]
[…] Laravel Create Zip File and Download Example: A Comprehensive Guide […]