How to Create a Zip file in PHP


How to Create a Zip file in PHP

Demo Download

In this tutorial, I will show you how to create a zip file in PHP and add multiple files and folders in that zip file.

If you are working on a PHP project and you need to create or use zip archive file then you are on the right place.

You will get the detail information about how to create a zip file and add files and folders in your zip file. You can also download that zip file if needed.

Example #1: Create a Zip File in PHP and Add Files & Folders

Below is the quick code to create a zip file in PHP and add files and folders in it.

<?php
// Check if php zip extension is enabled
if(extension_loaded('zip'))
{
    $zip = new ZipArchive(); // Create an instance of zip archive
    // Open a zip archive file
    if($zip->open('filename.zip', ZIPARCHIVE::CREATE)===TRUE)
    {
        $zip->addFile("filename.txt"); // Add a file to newly created zip archive
        $zip->addEmptyDir("folder_name"); // Add new empty folder in zip archive
        $zip->close(); // Close the zip archive
    }
    else
    {
        echo "Sorry unable to open zip archive file!";
    }
}
else
{
    echo "ZIP extension is not enabled!";
}
?>

The above code will create a zip file with name as filename.zip and it will add filename.txt text file and folder_name new empty folder in it.

Make sure that filename.txt is present in your application directory where you keep this PHP code file.

If your files are in some other directory, for example in files/ folder then you can use the following:

$directory = "files/";
$zip->addFile($directory."filename.txt"); // Add a file along with directory

You can add multiple files by repeating $zip->addFile(“filename2.txt”); and change file name in it.

Similarly you can add multiple folders or directories by repeating $zip->addEmptyDir(“folder_name2”); and enter your new folder name in it.

You should do it before closing the zip archive. This above code is enough to generate a zip file in PHP.

Previously, I have shared a tutorial on how to create a zip file using PHP and download multiple files.

But in this tutorial, I will share more detailed examples to cater different requirements of your application.

What is a ZIP Archive?

ZIP is an archive file format which is mainly use to combine multiple files of a folders and compress the data. However, you can add only one file or folder in it.

It is easy to share multiple files via zip archive and a recommended way to share multiple files over the internet. However, there is no restriction to share single file in it.

User can download several files individually but it takes too much time, therefore if you need to share multiple files with user so use zip archive file format.

Can I Create Multiple Zip Files with Same Name?

Well the quick answer of this question is NO, if you create the multiple zip files with same name then it will simply replace the existing zip file with the new one.

You can check it out by creating a new zip file, then make a little changes in your filename.txt text file and then again create a zip file.

It will simply replace the zip file and new zip file will contain the updated version of filename.txt text file.

So you can create your zip file with a unique name, a good example to create a unique zip file name is below:

<?php
$zip_filename = time().".zip";
echo $zip_filename; // It will be something like 1683206494.zip
?>

The above code will give you a unique name for your zip file so that it does not replace the previous zip file in the same directory or folder.

Example #2: Create a Zip File in PHP, Add File with New Name in Zip

You can create a zip file in PHP and add file in it, by default it adds the file with the same name. However, you can define the new name of that file in your zip file.

addFile('filename.txt', 'new_filename_in_zip.txt');

You will need to pass first argument as the real file which you want to add and second argument will be the new file name in your zip file.

You actually define the filename in zip archive as a second argument in addFile() function.

Complete Example Code:

// Check if php zip extension is enabled
if(extension_loaded('zip'))
{
    $zip = new ZipArchive(); // Create an instance of zip archive
    // Open a zip archive file
    if($zip->open('filename.zip', ZIPARCHIVE::CREATE)===TRUE)
    {
        // Add a file to newly created zip archive
        // and assign a new file name in zip file
        $zip->addFile("filename.txt", "new_filename_in_zip.txt"); 
        $zip->close(); // Close the zip archive
    }
    else
    {
        echo "Sorry unable to open zip archive file!";
    }
}
else
{
    echo "ZIP extension is not enabled!";
}

Example #3: Create a Zip File in PHP, Automatically Add File in New Folder

If you want to create a zip file in PHP and want to automatically create a new folder and add file in it, then you can use the below example code.

addFile("filename.txt", "files/filename.txt"); 

The above code will add file in zip in files folder/directory.

Complete Example Code:

// Check if php zip extension is enabled
if(extension_loaded('zip'))
{
    $zip = new ZipArchive(); // Create an instance of zip archive
    // Open a zip archive file
    if($zip->open('filename.zip', ZIPARCHIVE::CREATE)===TRUE)
    {
        // Add a file to newly created zip archive
        // add the new folder name along with filename to automatically add file in it
        $zip->addFile("filename.txt", "files/filename.txt");  
        $zip->close(); // Close the zip archive
    }
    else
    {
        echo "Sorry unable to open zip archive file!";
    }
}
else
{
    echo "ZIP extension is not enabled!";
}

Example #4: Select Files from Sub-Domain & Create Zip

What if you need to select files from your sub-domain directory and create a zip file of it.

Well you can easily select files from your any sub-domains, you just need to use the Home Directory for it.

When you logged in to your hosting cPanel, you will see your home directory path on the right sidebar.

General Information -> Home Directory.

Zip Home Directory

You will simply need to mention the home directory and sub-domain in your $directory variable like below:

$directory = "/home/your_home_directory/subdomain.domain.com/files/";
$zip->addFile($directory."filename.txt"); // Add a file along with home directory

We are simply now selecting files from your sub-domain and creating a zip file of it. In this way, you can select files from any sub-domain or main domain.

Complete Example Code:

// Check if php zip extension is enabled
if(extension_loaded('zip'))
{
    $zip = new ZipArchive(); // Create an instance of zip archive
    // Open a zip archive file
    if($zip->open('filename.zip', ZIPARCHIVE::CREATE)===TRUE)
    {
        $directory = "/home/your_home_directory/subdomain.domain.com/files/";
        $zip->addFile($directory."filename.txt"); // Add a file along with home directory
        $zip->close(); // Close the zip archive
    }
    else
    {
        echo "Sorry unable to open zip archive file!";
    }
}
else
{
    echo "ZIP extension is not enabled!";
}

Conclusion

This tutorial have taught you how to create a zip file in PHP with 4 different examples. Creating a Zip file using PHP extension is very easy.

I believe these examples will help you to complete your project with zip file creation without any error.

If you found this tutorial helpful, share it with your friends and developers group.

I spent several hours to create this tutorial, if you want to say thanks so like my page on Facebook, Twitter and share it.

Demo Download

Facebook Official Page: All PHP Tricks

Twitter Official Page: All PHP Tricks

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.

Leave a Reply

Your email address will not be published. Required fields are marked *