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

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

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

Why Your Business Needs a DevOps Consultant for Seamless Digital Transformation

In today’s fast-paced digital world, businesses must adopt innovative approaches to remain competitive. One of the most critical strategies for achieving operational efficiency and agility is implementing…

Test Database Connection is ok or not

Laravel provides an interactive shell called Tinker, which allows you to test database connections easily. Run: Then, in the interactive shell, try the following: If your connection…

The Ultimate Guide to Hiring the Best DevOps Freelancers for Your Business

In today’s fast-paced digital landscape, businesses are increasingly relying on DevOps freelancers to optimize their software development and IT operations. With the flexibility of freelance professionals, companies…

Master DevOps Skills with the Best DevOps Training Program

In today’s fast-paced IT landscape, DevOps has become a game-changer for businesses aiming to streamline software development and operations. Organizations are increasingly adopting DevOps methodologies to enhance…

Find the Best DevOps Trainers to Accelerate Your IT Career

The demand for skilled DevOps professionals is skyrocketing as organizations strive for faster development cycles, improved collaboration, and enhanced automation. Whether you are an IT professional looking…

Boost Your IT Career with the Best DevOps Courses in 2025

In today’s fast-paced digital landscape, DevOps has become an essential practice for organizations aiming to streamline their software development and IT operations. With businesses increasingly adopting cloud…

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