How to create and download a Zip file with PHP

PHP provides ZipArchive Class which allows us to create a Zip file. This class makes the file creation little easier.

Programmatically Zip creation mainly requires when preparing the group of file and folders for downloading.

In the demonstration, I am creating a function that will read all files and folders from the specified directory and add them in the ZipArchive class object.

How To Create And Download A Zip File with PHP


  1. HTML
  2. PHP
  3. CSS
  4. Conclusion


Creating a <form> which have two button elements, one for Create Zip and another for Download.

Completed Code

<div class='container'>
 <h1>Create and Download Zip file using PHP</h1>
 <form method='post' action=''>
   <input type='submit' name='create' value='Create Zip' />&nbsp;
   <input type='submit' name='download' value='Download' />

2. PHP

I have created includes folder within the project where I stored some files and folders.

Create Zip

Create ZipArchive Class object for Zip file creation. Define a function createZip() to read files and directory from the specified directory path.

If file

If the reading value is the file then add it to zip object using addFile() method.

If directory

If the value is directory then create an empty directory and call createZip() function where pass the directory path.

Download Zip

Check if zip file exists or not. If it exists then download and remove it from the server.

Completed Code

// Create ZIP file
  $zip = new ZipArchive();
  $filename = "./";

  if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");

  $dir = 'includes/';

  // Create zip


// Create zip
function createZip($zip,$dir){
  if (is_dir($dir)){

    if ($dh = opendir($dir)){
       while (($file = readdir($dh)) !== false){
         // If file
         if (is_file($dir.$file)) {
            if($file != '' && $file != '.' && $file != '..'){
            // If directory
            if(is_dir($dir.$file) ){

              if($file != '' && $file != '.' && $file != '..'){

                // Add empty directory

                $folder = $dir.$file.'/';
                // Read data of the folder

// Download Created Zip file
  $filename = "";

  if (file_exists($filename)) {
     header('Content-Type: application/zip');
     header('Content-Disposition: attachment; filename="'.basename($filename).'"');
     header('Content-Length: ' . filesize($filename));

     // delete file

3. CSS

   margin: 0 auto;
   width: 50%;
   text-align: center;

   border: 0px;
   padding: 7px 15px;
   font-size: 16px;
   background-color: #00a1a1;
   color: white;
   font-weight: bold;

4. Conclusion

The above-defined function createZip() accepts the path of the directory as a parameter. It will read all files and folders from the path and add in the zip file.

If you found this tutorial helpful then don't forget to share.

Related Post

Spread the love
  • 1


  1. Camilo said:

    I have this issue :
    ZipArchive::close(): Failure to create temporary file: Permission denied in…
    you know what could I do? I’m working on localhost

    April 19, 2017
  2. Very helpful article.
    But I had some question. How can I implement this code in static website? That website only have html, css and js code , how to do a creating zip file functionality.

    Thanks Advance for reply.

    April 21, 2017
  3. Nancy said:

    Thank you so very much!!!

    February 19, 2018
  4. Adem GENÇ said:

    Thank you very much.
    Working fast

    October 31, 2018

Leave a Reply

Your email address will not be published. Required fields are marked *