Introduction to Working with ZIP Archives in PHP
Data archiving is one of the basic tasks of web development. Whether it's backing up a database, packaging user-uploaded files, or exporting a large amount of information, the ZIP format remains the most popular and versatile solution. PHP provides a built-in ZipArchive class that allows you to perform all basic operations with ZIP archives without installing additional extensions.
In this article, we will cover in detail how to use ZipArchive in PHP: from creating a simple archive to handling nested folders and password protection. All code examples are ready to use on your server.
Preparation: Checking the Zip Extension
Before you start, make sure the zip extension is enabled on your server. It is usually installed by default in modern versions of PHP (7.4+, 8.x). You can check this using the phpinfo() function or a simple script:
<?phpif (extension_loaded('zip')) { echo 'Zip extension is available';} else { echo 'Zip extension not found. Install it via php.ini';}?>Creating a ZIP Archive in PHP
Step 1. Initializing the ZipArchive Object
To get started, create an instance of the ZipArchive class and open a new archive using the open() method. The second parameter — the ZipArchive::CREATE flag — indicates that the archive will be created anew (or overwritten if it already exists).
<?php$zip = new ZipArchive();$zipName = 'example.zip';
if ($zip->open($zipName, ZipArchive::CREATE) !== TRUE) { exit("Could not create archive $zipName");}?>Step 2. Adding Files to the Archive
The addFile() method adds an existing file from the file system. The first argument is the path to the file, the second (optional) is the name of the file inside the archive (if you want to rename it).
<?php// Add a single file$zip->addFile('document.pdf', 'my_document.pdf');
// Add multiple files$files = ['photo1.jpg', 'photo2.jpg', 'photo3.jpg'];foreach ($files as $file) { $zip->addFile($file);}?>Step 3. Adding an Entire Folder While Preserving the Structure
If you need to package a directory with all its nested files, use a recursive traversal. Here is a ready-made function:
<?phpfunction addFolderToZip($zip, $folder, $parentFolder = '') { $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($folder), RecursiveIteratorIterator::LEAVES_ONLY );
foreach ($files as $file) { if (!$file->isDir()) { $filePath = $file->getRealPath(); $relativePath = $parentFolder . $file->getFilename(); $zip->addFile($filePath, $relativePath); } }}
// Usage:$zip->addEmptyDir('my_folder'); // create a folder in the archiveaddFolderToZip($zip, '/path/to/source_folder', 'my_folder/');?>Step 4. Closing the Archive
After adding all files, be sure to close the archive — otherwise the data will not be written.
<?php$zip->close();echo "Archive $zipName successfully created!";?>Extracting a ZIP Archive in PHP
To extract files, use the extractTo() method. Specify the target folder where the files will be extracted.
<?php$zip = new ZipArchive();if ($zip->open('example.zip') === TRUE) { $zip->extractTo('/path/to/extract_folder/'); $zip->close(); echo 'Archive successfully extracted';} else { echo 'Error opening archive';}?>Advanced ZipArchive Features
Adding a String as a File
The addFromString() method allows you to create a file directly from a str