Sunday, April 21, 2024
HomePHPFind out how to Create Zip Information utilizing PHP ZipArchive and Obtain

Find out how to Create Zip Information utilizing PHP ZipArchive and Obtain

by Vincy. Final modified on September twelfth, 2022.

Creating a zipper from a folder stuffed with information will be executed in PHP utilizing the ZipArchive class. This class occasion creates a deal with to learn or write information to a compressed archive.

This class contains a number of properties and strategies to zip file archives.

On this article, we are going to see an instance of,

  1. Find out how to create a zipper archive file.
  2. Find out how to obtain the compressed zip file.

If you wish to know easy methods to compress multiple picture in PHP picture compression consult with this earlier article.

Find out how to create a zipper archive file

This file parses the enter listing and compresses its information into a zipper file. It proceeds with the next steps to create the zip file of a listing.

  1. Create a PHP ZipArchive class occasion.
  2. Open a zipper file archive with the occasion. It accepts the output zip file identify and the mode to open the archive.
  3. Apply a recursive parsing within the enter listing.
  4. If the listing features a file, then it provides to the zip archive utilizing addFile().

It handles the use circumstances of getting the chances of being unable to learn or archive the listing. As soon as the zip is created, it shows a message to the browser.


// Necessary: You need to have learn and write permissions to learn
// the folder and write the zip file
$zipArchive = new ZipArchive();
$zipFile = "./";
if ($zipArchive->open($zipFile, ZipArchive::CREATE) !== TRUE) {
    exit("Unable to open file.");
createZip($zipArchive, $folder);
echo 'Zip file created.';

operate createZip($zipArchive, $folder)
    if (is_dir($folder)) {
        if ($f = opendir($folder)) {
            whereas (($file = readdir($f)) !== false) {
                if (is_file($folder . $file)) {
                    if ($file != '' && $file != '.' && $file != '..') {
                        $zipArchive->addFile($folder . $file);
                } else {
                    if (is_dir($folder . $file)) {
                        if ($file != '' && $file != '.' && $file != '..') {
                            $zipArchive->addEmptyDir($folder . $file);
                            $folder = $folder . $file . "";
                            createZip($zipArchive, $folder);
        } else {
            exit("Unable to open listing " . $folder);
    } else {
        exit($folder . " just isn't a listing.");


//If succeeded it returns 
Zip file created.

//If failed it returns 
Unable to open listing example-folder.
"example-folder just isn't a director.

php create zip

Find out how to obtain the compressed zip file

Within the final step, the zip file is created utilizing the PHP ZipArchive class. That zip file will be downloaded through the use of the PHP code beneath.

It follows the beneath steps to obtain the zip file created.

  1. Get absolutely the path of the zip file.
  2. Set the header parameters like,
    • Content material size.
    • Content material kind.
    • Content material encoding, and extra.


$filename = "";
if (file_exists($filename)) {
    // modify the beneath absolute file path in accordance with the folder you have got downloaded
    // the zip file
    // I've downloaded the zip file to the present folder
    $absoluteFilePath = __DIR__ . "" . $filename;
    header('Pragma: public');
    header('Expires: 0');
    header('Cache-Management: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Management: personal', false);
    // content-type needs to be outlined in accordance with the file extension (filetype)
    header('Content material-Kind: utility/zip');
    header('Content material-Disposition: attachment; filename="' . basename($filename) . '";');
    header('Content material-Switch-Encoding: binary');
    header('Content material-Size: ' . filesize($absoluteFilePath));

This file simply has the hyperlinks to set off the operate to create a zipper file containing the compressed archive of the listing. Then, the motion to obtain the output zip archive is known as.


<div class="container">
    <h2>Create and Obtain Zip file utilizing PHP</h2>
        <a href="">Create Zip File</a>
        <a href="download-zip-file.php">Obtain Zip File</a>

Some strategies of PHP ZipArchive class

We are able to do extra operations through the use of the strategies and properties of the PHP ZipArchive class. This checklist of strategies is supplied by this PHP class.

  1. rely() – used to get the variety of information within the zip archive file.
  2. extractTo() – extracts the archive content material.
  3. renameIndex() – rename a selected archive entry by index.
  4. replaceFile() – change a file within the zip archive with a brand new file by specifying a brand new path.

ZipArchive strategies used on this instance

A number of the strategies are used on this instance listed beneath. These are incessantly used strategies of this class to work with this.

  1. open() – Open a zipper archive file by specifying the .zip file identify.
  2. addFile() – So as to add a file from the enter listing to the zip archive.
  3. addEmptyDir() – provides an empty listing into the archive to load the subdirectory file of the enter listing.
  4. shut() – closes the energetic ZipArchive with the reference of the deal with.


↑ Again to High



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments