Hi artisan when you navigate file in server with your browser and there are many files but you want them zip with a single file so that you easily download with one click attachment is download to your computer. Today I will show you how to create zip file in Laravel.
You can use other package over the interent to make zip file, but this tutorial we will use ziparchive without install 3rd party package to perform this task. Laravel provide ZipArchive class for create zip file in laravel which is built-in I will show very short and quick to implement this.
We are going to download a fresh Laravel app by runing follow command, if you not yet install composer please read this from sctrach to install Laravel in your computer.
composer create-project laravel/laravel blog
Now we will generate a controller file to handle Zip Archive file. Running following command to create the file with name ZipController.php
Open terminal run command
php artisan make:controller ZipController
Open file app -> Http -> Controllers -> ZipController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use ZipArchive;
public function ZipController()
{
public function index()
{
$zip = new ZipArchive;
$fileName = 'Document.zip';
if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
{
$files = \File::files(public_path('ToZip'));
foreach ($files as $key => $value) {
$file = basename($value);
$zip->addFile($value, $file);
}
$zip->close();
}
return response()->download(public_path($fileName));
}
}
Make sure you have add some files in folder ToZip which is in public path.
We will add route to web.php to tell where controller and method point to correct URL.
Open file routes -> web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ZipController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/zip', [ZipController::class, 'index']);
To see the result we have to activate Laravel server and navigate to correct route link which we just implementation. But first, you may run following command to start server
php artisan serve
Then you can navigate your browser to http://localhost:8000/zip your file will download as Zip file.
You Might Also Like...
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him