How to Zip a directory in PHP? Last Updated : 10 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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 <?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 <?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. Comment More infoAdvertise with us Next Article How to Zip a directory in PHP? M Magisk Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads How to count files in a directory using PHP? PHP contains many functions like count(), iterator_count(), glob(), openddir(), readdir(), scandir() and FilesystemIterator() to count number of files in a directory.count() Function: The count() functions is an array function which is used to count all elements in an array or something in an object 3 min read How to include() all PHP files from a directory ? Given list of PHP files in a directory and the task is to include all files from a directory. In order to include all PHP files from directory at once need a foreach-loop. Example: This example contains four PHP files (file1.php, file2.php, file3.php, file4.php) in a directory. Create one file named 1 min read How to copy a file from one directory to another using PHP ? The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure. Syntax: bool 2 min read How to execute PHP code using command line? PHP is mainly used for creating dynamic websites, but you can also run PHP scripts without a web server. This can be done using the PHP Command Line Interface (PHP CLI), which allows you to execute PHP scripts directly from the command line. This method is commonly used for testing, automation, or r 3 min read How to Convert Byte Array to String in PHP? Given a Byte Array, the task is to convert the byte array to a String in PHP. It is used in various scenarios, such as processing binary data, handling file uploads, or working with data transmission protocols. Below are the approaches to convert byte array to string in PHP:Table of ContentWhat is a 3 min read Like