How to Create Zip File and Download in Laravel 10

  Mar 2024
  Category: Laravel
How to Create Zip File and Download in Laravel 10

Welcome To ITSolutionsGuides,

In Laravel 10, efficiently managing file downloads is essential, especially when dealing with multiple files. To create and download zip files seamlessly, first, ensure Laravel 10 is installed. Then, define a route in routes/web.php pointing to a method in a new controller, say ZipController. In the controller, create a method that gathers files from a specified folder, uses ZipArchive to compress them into a zip file, and then returns a download response to the user. This response triggers the download of the generated zip file. Ensure proper error handling in case of failures. Test the route by accessing it via the browser. By following this approach, you can easily implement zip file creation and downloading functionality in Laravel 10, facilitating efficient management and distribution of files within your application using it solutions guides.
lets see to create and download Zip files effortlessly in Laravel 10 using ZipArchive. Zip files from folders and send download response seamlessly using ZipArchive.

Let's Start Coding

Lets create the required routes for the project,


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('download-zip', ZipController::class);

Let's Create Controller

Lets create the controller using the following artisan command,
php artisan make:controller ZipController

for detailed explanation about creating the controller see here
In the controller we will use the ZipArchive class to create the zip file . We also need to provide the zipfile name and then the folder name in which folder files to be get zipped. The folder should be in the public folder of the laravel project.


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use File;
use ZipArchive;
class ZipController extends Controller
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function __invoke()
        $zip = new ZipArchive;
        $fileName = '';
        if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
            $files = File::files(public_path('myFiles'));
            foreach ($files as $key => $value) {
                $relativeNameInZipFile = basename($value);
                $zip->addFile($value, $relativeNameInZipFile);
        return response()->download(public_path($fileName));

Let's Run The Project

Lets run the project using the following artisan command,

php artisan serve

and the enter the below url in the browser the zip file will automatically gets downloaded.


We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.


ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides