Php zip a directory and automatically download
Introduction
This guide explains how to use PHP to compress an entire directory into a ZIP file and automatically download the generated ZIP file through the browser. This is useful when users need to download multiple files or folders as a single archive.
Prerequisites
Before implementing this functionality, ensure the following requirements are available:
- PHP is installed and running on the server.
- The PHP
ZipArchiveextension is enabled. - The directory to be compressed exists and is readable by the PHP process.
- The web server has permission to create the ZIP file in the required location.
- Sufficient disk space is available for the ZIP archive.
You can verify whether the ZIP extension is enabled using:
php -m | grep zip
Assumption value:
$sourecedir = 'images/';//this is an path of the folder name $zip_file = 'file.zip';//this is an downloadable zip foler name
There two variable $sourcedir and $zipfile, $sourcedir varible mention the folder and $zipfile mention downloadable folder name.
First, initialise the archive object
$zip = new ZipArchive();// Initialize archive object> $zip = new ZipArchive(); $zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);
Second, create a recursive directory iterator is download all files with subfoler
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath)
);
Third, use a foreach loop to add the file to the archive
foreach ($files as $name => $file)
{
if (!$file->isDir())
{
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
$zip->addFile($filePath, $relativePath);
}
}
Finally, close the zip and add zip header in PHP,read the file
$zip->close();
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($zip_file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($zip_file));
readfile($zip_file);
If you follow all the steps and get the final code like this
<?php
$dir = 'target';
$zip_file = 'file.zip';
$rootPath = realpath($dir);
$zip = new ZipArchive();
$zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath)
);
foreach ($files as $name => $file)
{
if (!$file->isDir())
{
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($zip_file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($zip_file));
readfile($zip_file);
?>
Conclusion
Using PHP’s <code dir=”ltr”>ZipArchive class provides a simple way to compress a directory and automatically download it through the browser. The approach recursively includes files and subdirectories, creates a ZIP archive, sends it to the user, and removes the temporary archive after the download.
