How to Zip a directory in PHP?
ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. The PHP ZipArchive class can be used to zipping and unzipping. It might be need to install the class if it is not present.
Installation for Linux users:
In order to use these functions you must compile PHP with zip support by using the –enable-zip configure option.
Installation for Windows users:
As of PHP 5.3 this extension is inbuilt. Before, Windows users need to enable php_zip.dll inside of php.ini in order to use its functions.
Example: This example uses ZipArchive class and create a zipped file.
<?php // Enter the name of directory $pathdir = "Directory Name/"; // Enter the name to creating zipped directory $zipcreated = "Name of Zip.zip"; // Create new zip class $zip = new ZipArchive; if($zip -> open($zipcreated, ZipArchive::CREATE ) === TRUE) { // Store the path into the variable $dir = opendir($pathdir); while($file = readdir($dir)) { if(is_file($pathdir.$file)) { $zip -> addFile($pathdir.$file, $file); } } $zip ->close(); } ?> |
Example: This example uses ZipArchive class to unzip the file or directory.
<?php // Create new zip class $zip = new ZipArchive; // Add zip filename which need // to unzip $zip->open('filename.zip'); // Extracts to current directory $zip->extractTo('./'); $zip->close(); ?> |
Steps to run the program: Zip a directory ‘zipfile’ containing a file ‘a.txt’.
- Save the above code into two files with extension .php .One for zip and another for unzip the directory. Also give the appropriate path for the directory.
- Here we use XAMPP to run a local web server. Place the php files along with the directory to be zipped in C:\xampp\htdocs(XAMPP is installed in C: drive in this case).

- In the browser, enter https://localhost/zip.php as the url and the file will be zipped.

- After this a new zip file is created named ‘file’.

Similarly, do the same to unzip. Make sure the filename and path are matching. The text file a.txt is extracted from the zip file.

Recommended Posts:
- Copy the entire contents of a directory to another directory in PHP
- PHP | dir() (Get instance of the Directory)
- How to include() all PHP files from a directory ?
- How to create directory with HTML button and PHP ?
- Perl | Accessing a Directory using File Globbing
- How to recursively delete a directory and its entire contents (files + sub dirs) in PHP?
- How to create Responsive Bottom Navigation Bar using Bootstrap ?
- How to save an HTML 5 Canvas as an image on the server ?
- How to accordion scroll to top to open content in Bootstrap ?
- PHP | Imagick paintTransparentImage() Function
- p5.js | remove() Function
- p5.js | hide() Function
- p5.js | curveDetail() Function
- CSS | scale3d() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



