Create a Zip File and Download in Laravel 10

Introduction

In this tutorial, we will learn how to create a zip file and download it in Laravel 10. We will explore the step-by-step process with a live example. Are you ready? Let’s dive in!

Step 1: Set up a Laravel project

First, let’s create a new Laravel project by running the following command in your terminal:

composer create-project laravel/laravel example-app

Step 2: Create a route

Next, let’s create a route in the web.php file located in the routes directory. Open the file and add the following code:

Route::get('/download-zip', [DownloadController::class, 'downloadZip']);

Step 3: Create a controller

Now, let’s create a controller called DownloadController. Run the following command in your terminal:

Open the newly created DownloadController.php file in the app/Http/Controllers directory and add the following code:

use ZipArchive;

class DownloadController extends Controller
{
    public function downloadZip()
    {
        // Create a new zip archive
        $zip = new ZipArchive;
        
        // Path to the directory you want to compress
        $directory = public_path('files');
        
        // Path to the zip file you want to create
        $zipFile = public_path('downloads/files.zip');
        
        // Open the zip file for writing
        if ($zip->open($zipFile, ZipArchive::CREATE) === TRUE) {
            
            // Add all files from the directory to the zip file
            $files = File::allFiles($directory);
            
            foreach ($files as $file) {
                $zip->addFile($file->getPathname(), $file->getBasename());
            }
            
            // Close the zip file
            $zip->close();
        }
        
        // Download the zip file
        return response()->download($zipFile);
    }
}

Step 4: Create a view

php artisan make:view download
<!DOCTYPE html>
<html>
<head>
    <title>Download Zip File</title>
</head>
<body>
    <h1>Download Zip File</h1>
    
    <a href="{{ url('/download-zip') }}">Click here to download the zip file</a>
</body>
</html>

Step 5: Update the route

Route::get('/download', function () {
    return view('download');
});

Output:-

Related Posts

Professional Skill Alignment Around MLOps Foundation Certification in Modern Workplaces

Introduction The MLOps Foundation Certification has emerged as a critical benchmark for professionals looking to bridge the gap between data science and production engineering. This guide is…

Read More

Certified AIOps Manager: Strategic Framework for Intelligent IT Operations

Introduction The Certified AIOps Manager program is a specialized training designed to help professionals lead the next wave of IT operations. This guide is for engineers and…

Read More

Advanced AIOps Architect Certification Roadmap for DevOps Engineers

Introduction The Certified AIOps Architect is a comprehensive professional program designed for engineers and architects who want to master the intersection of Artificial Intelligence and IT Operations….

Read More

Advanced Certified AIOps Professional Guide for Mastering AI Driven Operations Skills

Introduction Artificial Intelligence for IT Operations is the future of managing complex systems and large scale digital environments. The Certified AIOps Professional program is designed for those…

Read More

Certified AIOps Engineer Training to Boost Automation Monitoring and Career Growth

The Certified AIOps Engineer is a specialized professional program designed to integrate artificial intelligence into modern IT operations. As systems scale and generate massive amounts of telemetry…

Read More

Advanced Guide to AIOps Foundation Certification for Scalable IT Infrastructure

In an era where infrastructure and applications generate massive amounts of telemetry data, manual intervention is no longer a sustainable strategy for maintaining system uptime. The AIOps…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x