Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Create Zip Files and Enable Download Functionality in Laravel 9

File compression and archiving are common tasks in web development, often required to bundle multiple files into a single, downloadable archive. In Laravel 9, a powerful PHP framework, achieving this functionality is straightforward using the ZipArchive class.

The code snippet you’ve provided is an example of how to create a zip file containing multiple files and offer it for download. Let’s dissect the code step by step.

public function __invoke()
{
    $zip = new ZipArchive;

    $fileName = 'myNewFile.zip';
 
    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);
        }
           
        $zip->close();
    }
  
    return response()->download(public_path($fileName));
}

Here’s what each part of the code does:

$zip = new ZipArchive;: This line initializes a new ZipArchive object, which provides the functionality to work with zip archives.

$fileName = 'myNewFile.zip';: Here, you define the name of the zip file you want to create. You can customize this to suit your needs.

Opening the Zip Archive: The if condition checks if the zip archive can be opened for writing. If successful, it proceeds to add files to the archive.

Adding Files to the Zip Archive: Inside the loop, the code iterates through the files in the myFiles directory and adds each file to the zip archive. It uses addFile to specify the source file and the relative path within the archive.

Closing the Zip Archive: After adding all the files, the zip archive is closed using the close method.

return response()->download(public_path($fileName));: Finally, the code returns a response to download the created zip file. The public_path() function is used to specify the file path.

Usage Example

To use this code, you can create a route or controller method that invokes the code when a specific URL is accessed. For instance, if you want users to download a zip file by visiting example.com/download-zip, you can set up a route and controller for that URL.

Related Posts

The Complete 2025 Guide to GitLab Training, Certification, and Expert Trainers

Level Up Your DevOps Career: The Complete 2025 Guide to GitLab Training, Certification, and Expert Trainers Introduction to GitLab: The Backbone of Modern DevOps As businesses accelerate…

Site Reliability Engineering (SRE) Foundation Certification

Introduction to Site Reliability Engineering (SRE) Foundation Certification The Site Reliability Engineering (SRE) Foundation certification is an industry-recognized credential designed to provide students with a comprehensive understanding…

DevOps Foundation Certification

Introduction to DevOps Foundation Certification The DevOps Foundation Certification is a crucial credential designed for individuals looking to master the core principles of DevOps and its practical…

Understanding and Fixing the “Update minSdk Version Error” in Flutter

When working with Flutter, you may occasionally encounter the dreaded “Update minSdk Version Error”. This error typically arises when the Android project within your Flutter app targets…

Medical Tourism in the Digital Era: Top Destinations & the Platforms Powering Global Patient Access

As healthcare grows more expensive and less accessible in many parts of the world, a powerful alternative is rising—medical tourism. From elective cosmetic surgeries to life-saving cardiac…

Understanding and Protecting Against XSS (Cross-Site Scripting) Attacks

Cross-Site Scripting (XSS) remains one of the most common and dangerous security vulnerabilities in web applications. It allows attackers to inject malicious scripts into webpages viewed by…

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