Learn how to create ZIP file using PHP. In the previous post, we will see how to compress the image before uploading in PHP.
Zip file normally used to compress folder and files and create an archive file which allows downloading at all files at once.
In PHP we can create ZIP file dynamically and save archive files on the server easily.
You can archive the full directory recursively to the ZIP file in PHP. In this tutorial, we will show you the best practices to make ZIP file from folder using PHP.
Example: Create ZIP File Using PHP ZipArchive
Step 1: Add ZipArchiver Class:
The ZipArchiver class creates ZIP file from a folder on the server with the help of PHP ZipArchive.
zipDir(): This function creates a Zip of a folder recursively including the parent directory.
$sourcePath: Folder path to be zipped.
$outZipPath: Path to save the zip file on the server.
dirToZip(): It is a helper function of ZipArchiver class that adds folders and files to the zip file.
Create a ZipArchiver.class.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php Class ZipArchiver { public static function zipDir($sourcePath, $outZipPath){ $pathInfo = pathinfo($sourcePath); $parentPath = $pathInfo['dirname']; $dirName = $pathInfo['basename']; $z = new ZipArchive(); $z->open($outZipPath, ZipArchive::CREATE); $z->addEmptyDir($dirName); if($sourcePath == $dirName){ self::dirToZip($sourcePath, $z, 0); }else{ self::dirToZip($sourcePath, $z, strlen("$parentPath/")); } $z->close(); return true; } /** * Add files and sub-directories in a folder to zip file. */ private static function dirToZip($folder, &$zipFile, $exclusiveLength){ $handle = opendir($folder); while(FALSE !== $f = readdir($handle)){ // Check for local/parent path or zipping file itself and skip if($f != '.' && $f != '..' && $f != basename(__FILE__)){ $filePath = "$folder/$f"; // Remove prefix from file path before add to zip $localPath = substr($filePath, $exclusiveLength); if(is_file($filePath)){ $zipFile->addFile($filePath, $localPath); }elseif(is_dir($filePath)){ // Add sub-directory $zipFile->addEmptyDir($localPath); self::dirToZip($filePath, $zipFile, $exclusiveLength); } } } closedir($handle); } } |
Step 2: Create index.php to make ZIP in PHP
Include ZipArchiver.class.php to archive all sub-folder and files of the given folder and make ZIP file from the script in PHP.
- Include ZipArchive class and initialize it.
- Mention the directory path, which you want to archive as a ZIP file
- Add path to save the ZIP file on the server
- Call the zipDir() function to make ZIP file.
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php // Include and initialize ZipArchive class require_once 'ZipArchiver.class.php'; $zipper = new ZipArchiver; // Path of the directory to be zipped $dirPath = '/path/to/sourceDir'; // Path of output zip file $zipPath = '/path/to/archive-'.date('dmY').'-'.time().'.zip'; // Create zip archive $zip = $zipper->zipDir($dirPath, $zipPath); if($zip){ echo 'ZIP file created successfully.'; }else{ echo 'Failed to create ZIP.'; } |