Laravel Create Zip Archive with Files and Download it
Hello Artisans, today I’ll show you how to create zip archive with files and download it in our Laravel app without using any packages. Sometimes in our application, we want to let users download multiple files at one click, for this kind of operation it’s better to create one archive and let users download it. For achieved it we will use a ZipArchive class which is built-in since v5.2. But before using that we need to make sure that our php.ini has enabled the extension called ext-zip. So no more talk, let’s see how we can use it in our application.
We will first see how we can archive a file with ZipArchive class, see the below code
<?php
namespace App\Http\Controllers;
use ZipArchive;
class FrontendController extends Controller
{
public function index(): \Symfony\Component\HttpFoundation\BinaryFileResponse
{
$zip_file = 'invoices.zip'; // Name of our archive to download
// Initializing PHP class
$zip = new ZipArchive();
$zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$invoice_file = 'invoices/aaa001.pdf';
// Adding file: second parameter is what will the path inside of the archive
// So it will create another folder called "storage/" inside ZIP, and put the file there.
$zip->addFile(storage_path($invoice_file), $invoice_file);
$zip->close();
// We return the file immediately after download
return response()->download($zip_file);
}
}
It’s easy right!
Now we will see how we can archive the whole list of invoices from our storage folder
See the below code snippet
<?php
namespace App\Http\Controllers;
use ZipArchive;
class FrontendController extends Controller
{
public function index(): \Symfony\Component\HttpFoundation\BinaryFileResponse
{
$zip_file = 'invoices.zip';
$zip = new ZipArchive();
$zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$path = storage_path('invoices');
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
foreach ($files as $name => $file)
{
// We're skipping all subfolders
if (!$file->isDir()) {
$filePath = $file->getRealPath();
// extracting filename with substr/strlen
$relativePath = 'invoices/' . substr($filePath, strlen($path) + 1);
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
return response()->download($zip_file);
}
}
And you’re done. You don’t need to use any packages for that and it’s very easy to use too.
That’s all for today. Hope it can help you. Thanks for reading ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)