PHP

How to create dynamic zip Archive file in PHP?

In this tutorial I am going to provide solution on creating dynamic zip archive file in PHP, look at the following features of this tutorial.

Tutorial Features

  • Create Zip archive File
  • Add Files dynamically
  • Save Zip archive File on server
  • Download recently generated Zip Archive File From the Server

Let’s assume you have a task that required to collect all uploaded images, documents or text files of the user and zip those collection of document into on bundle and keep it ready for download.

Before diving into the tutorial if your running on PHP 7 and don’t have zip library installed then use following command to get it installed for ubuntu/linux users.

$ sudo apt-get install php7.0-zip

Step 1: Create new PHP Project

Create new PHP project directory into your PHP environment, you can skip this step if you are trying to implement this feature into your existing project.

Here I have created my PHP project and now I am going to add few dummy files to user for this demo, you can look at the following project folder structure to get idea

Project Folder Structure

So I as you see in the above folder structure we have bunch of text file ready to execute the demo, let’s move on to the next step

Step 2: Create Zip File with Existing Files

Assume that you have file-1.txt, file-2.txt are uploaded by you active users, now they want to download all files in bundle, that is our next step to simply add requested files into zip archive file and download a created zip files.

Let’s update our index.php file to add script to create our zip file and add files into it.

index.php:

<?php

/* 
 *  iTech Empires
 *  Author: Yogesh Koli <yogesh@itechempires.com>
 *  Website: www.itechempires.com
 */
$zip = new \ZipArchive();
$zip->open(__DIR__ . '/files/my-zip-file.zip', \ZipArchive::CREATE);

$zip->addFile('file-1.txt');
$zip->addFile('file-2.txt');
$zip->addFile('file-3.txt');
$zip->addFile('file-4.txt');

$zip->close();

Step 3: Zip File Download Script:

In this step we are going to write a php script to able to load a particular file from the folder and making it ready for download.

so here we have our zip file stored into the files folder next we just need to write a php script to open that file from the folder and download as an attachment.

Go ahead and open index.php file and add following script after closing of the zip object:

$file = __DIR__ . '/files/my-zip-file.zip';

header('Pragma: public');
header('Expires: -1');
header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');
header("Content-Disposition: attachment; filename=my-zip-file.zip");
header("Content-Type: application/zip");
header("Content-Length: " . filesize($file));
header("Content-Description: File Transfer");

if ($fp = fopen($file, 'rb')) {
    ob_end_clean();

    while (!feof($fp) and (connection_status() == 0)) {
        print(fread($fp, 8192));
        flush();
    }

    @fclose($fp);
}

Complete Script:

Here is complete script from index.php file, go ahead and run this file into your server of local development environment and let me know if this script helps you into your project.

<?php

/*
 *  iTech Empires
 *  Author: Yogesh Koli <yogesh@itechempires.com>
 *  Website: www.itechempires.com
 */
$zip = new \ZipArchive();
$zip->open(__DIR__ . '/files/my-zip-file.zip', \ZipArchive::CREATE);

$zip->addFile('file-1.txt');
$zip->addFile('file-2.txt');
$zip->addFile('file-3.txt');
$zip->addFile('file-4.txt');

$zip->close();

$file = __DIR__ . '/files/my-zip-file.zip';

header('Pragma: public');
header('Expires: -1');
header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');
header("Content-Disposition: attachment; filename=my-zip-file.zip");
header("Content-Type: application/zip");
header("Content-Length: " . filesize($file));
header("Content-Description: File Transfer");

if ($fp = fopen($file, 'rb')) {
    ob_end_clean();

    while (!feof($fp) and (connection_status() == 0)) {
        print(fread($fp, 8192));
        flush();
    }

    @fclose($fp);
}

?>

Thank you for being a valuable reader of iTech Empires, let me know if this solution works for you in your project by using comment box below.

Keep Learning, Happy Coding.

See you in next tutorial.

Yogesh Koli

Software engineer & Blogger live in India, has 8+ years of experience working with the Front-end and Back-end Web Application Development.

View Comments

  • Hi Yogesh, Good article, please can you explain that how can we create 'Password Protected Zip with above method.

Recent Posts

Complete guide of using Laravel 6 Eloquent Subquery Enhancements

Learn How to use laravel frameworks new improved feature called Eloquent Subquery and get example…

2 years ago

3 Useful examples of using Array Map function in PHP – Best Practices

Learn how to use php array map function with easy and essential tutorial to modify…

2 years ago

Working with PHP Array Filter Function – Best Practices

Learn how to use php array filter function with easy and essential tutorial to filter…

2 years ago

How to add Access Modifiers with Constructor Parameters in TypeScript

Want to know how to refactor your Typescript class, Learn here utilising Typescript of the…

2 years ago

What is Access Modifiers and how to use Access Modifiers in TypeScript ?

What is Access Modifiers in typescript, how to use Access Modifiers, when to use them,…

2 years ago

Top 10 Super Useful Packages to Improve Laravel applications in 2019

This tutorial provide ultimate list of package those are top 10 on packagist and super…

2 years ago