You can make a bundle of any files and convert them into a zip file. Laravel provides the flexibility to create a zip file. This will make it convenient for us to download more than one file together in a combined way. Either you can download multiple files one by one by clicking on the download button again and again. But it will be a time-consuming task for you. So, why don’t we try some easy and less time-consuming approach? Yes, you can do it in Laravel by creating a zip file. We will implement Laravel zip file download functionality in this post. I will show you, how you can make a zip file of any folder containing files. So, we are going to implement something as shown below.
Prerequisites
We will be implementing this functionality in Laravel 9. You can use this approach in the previous version of Laravel as well. For creating a Laravel 9 project, you will need to have the following configurations.
- PHP >=8.0.2
- Composer
- Apache/Nginx Server
- VS Code Editor (Optional)
- MySQL (version > 5)
I am assuming you are ready with a Laravel 9 project setup. If not then let’s create a new project setup.
Create Project For Implementing Laravel Zip File Download
For having a new project set up in Laravel, you can use the below command in the terminal.
composer create-project --prefer-dist laravel/laravel zip-app
Once, you have the project set up, let’s create a controller quickly.
Create a Controller to Download Zip File in Laravel 9
For creating a controller, switch back to the terminal and hit the below command.
php artisan make:controller ZipController
After creating the controller, you need some files in a folder which will be converted into zip file. So for the simplicity, I have created a new folder inside public folder of the project with the name flowers. Inside this folder, I have a couple of images which are going to convert into the zip file.
Next, you have to add the below snippet in this controller.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use File;
use ZipArchive;
class ZipController extends Controller
{
/**
* Function - Download File
* @param NA
* @return file
*/
public function downloadFile() {
$zip = new ZipArchive;
$fileName = 'flowers.zip';
if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
{
$files = \File::files(public_path('flowers'));
foreach ($files as $key => $value) {
$file = basename($value);
$zip->addFile($value, $file);
}
$zip->close();
}
return response()->download(public_path($fileName));
}
}
In the above controller, I have used PHP ZipArchive class for making Laravel zip file. Then inside the function, I have initialized that by creating it’s object. After that, it will create a zip file named flowers.zip and inside this zip file, I have added the flower images from the public flowers folder.
Create Route for Laravel Zip
For adding a route, navigate to the web.php and add the below route.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ZipController;
Route::get('download-zip', [ZipController::class, 'downloadFile']);
Once, you done, it’s time to check with the result.
Result of Laravel Zip File Download
Run the application and visit the URL in the browser to see the result. Once, you hit the URL, you will get this download prompt.
After completing the download, extract the zip file and check the files.
After extracted the folder, you can see the archived files are re-formed in the original files.
So, that’s it for this post. If you find any difficulty while implementing this functionality then you can reach out to me. Also, you can ask your doubts using the comment section here. I will try to resolve your query at earliest.
Leave a Reply