How to Create ZIP File using PHP


The ZIP is a normally used file format to archive files with information compression. When you need to enable the client to download numerous folders and files from the server, you have to make a ZIP file on the fly. It compresses files and archive to download numerous files at once. The ZIP file creation from the PHP script is basic as like the add to archive functionality in the System.

Making ZIP archive from the directory can be effectively executed utilizing PHP. The ZipArchive class in PHP gives a moment capacity to compress files or organizer in the ZIP file. You can archive the whole directory recursively to ZIP file utilizing PHP. In this instructional exercise, we will demonstrate to you best practices to make ZIP file from folder utilizing PHP.

Class for ZipArchiver:

The ZipArchiver class makes ZIP file from folder (files and sub-folders) on the server with the help of PHP ZipArchive.

Create ZipArchiver.class.php file and put below code:

zipDir() – this function helps to makes Zip of a folder recursively including the parent directory.

$sourcePath – Relative way of the directory to be zipped.

$outZipPath – output zip file Path .

dirToZip() – It is a helper function of class that include files and sub-directories in a folder to zip file.

Now Create ZIP in PHP

we will use ZipArchiver class to archive all files and sub-directories of the given folder and make ZIP file from the script in PHP.

  • Include ZipArchive class and initialize it.
  • Describe the path of the directory which you need to archive as a ZIP.
  • Describe the path to save or store the ZIP file on the server.
  • Call the zipDir() capacity of ZipArchiver class to make ZIP.

Create index.php file and write below code


Related posts