Creating zip file archives in php

In this post we are talking about to creating zip file in PHP. It’s very simple to create zip file, similar to creating in desktop. There is one more logic behind to creating zip file, if you want give some stuff for download to your users, then you have to upload completely zip file in your server then provide the download link over it.

But with the use of this function you just have to give the files that you want to store in zip and the name of the zip file that you want to create, yes you will create a single zip file with the collection of files. As you see in the below codes i have created a simple form and with the use of check-box i have given the choice for the files that you want to add in the zip file, and then all the rest work depending on the PHP.

HTML Part :

<form action="" method="POST">
<table border="0" align="center" width="150" cellpadding="4" cellspacing="4">
<tr>
<td><input type="checkbox" name="zipfile[]" value="images/lonely.JPG" /></td>
<td>JPG</td>
</tr>
<tr>
<td><input type="checkbox" name="zipfile[]" value="images/kaftar.JPG" /></td>
<td>JPG</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" name="zip_down" value="Download" /></td>
</tr>
</table>
</form>

 

creating zip file flow in PHP:

As you see in the coding part, i just simply used a function that will create the zip file. In this function i accepted two params : One in the collection of files/file whose added in the zip file and the second for the name of the zip file.

// Creating Zip file php
if( isset($_POST['zip_down']) ) {

    //create_zip($_POST['zipfile'], "myfile.zip");
    if( function_exists('create_zip') ) {
        create_zip($_POST['zipfile'], "myfile.zip");
    }
    else {
        echo "function not exists!";
    }
}

 

In the next section first of all we have to check the nested params. Then have to check that the extension for the ZIP file is loaded or not in PHP. Yes we need to enable the zip file extention from the php.ini file, and check the zip file extension with use of the PHP function ‘extension_loaded()’, this function return true on success.

if( extension_loaded('zip') ){

 

After that we have to create an object of the ‘ZipArchive()’ class. With this object we will call all the methods of ‘ZipArchive()’ class. As you see in the code i have called the open() method with the object. To know more about the ‘ZipArchive()’ class and its method please visit the php.net website. After open() method we will use the addFile() file method to add the files in the zip archives. This part create the zip file. Now next we have to create the download option with the use of header() function, and at the last remove the created zip file with use of unlink() function.

Here is the complete PHP code of the function:

function create_zip( $files = array(), $filename ){

    // Verify Array
    if( is_array($files) ) {

        if( count($files) ) {

            // First Check the Zip Lib
            if( extension_loaded('zip') ) {

                // Load Zip Library
               $zip = new ZipArchive();
               $zip_name = $filename;

              $zip_create = $zip->open( $zip_name, ZIPARCHIVE::CREATE );
              if( $zip_create === TRUE ) {
                  foreach( $files as $file ){
                  $zip->addFile($file);
              }
          }

        $zip->close();

        if( file_exists( $zip_name ) ) {

            header('Content-type: application/zip');
            header('Content-Disposition: attachment; filename="'.$zip_name.'"');
            readfile($zip_name);

            // Remove Zip File from Location
            unlink($zip_name);
       }

    }
    else echo "Extension Not Enabled!";

}
}
else{
echo "There is no file to create ZIP!";
}

}

 

Posted by Jogesh Sharma

Jogesh Sharma is a web developer and blogger who loves all the things design and the technology, He love all the things having to do with PHP, WordPress, Joomla, Magento, Durpal, Codeigniter, jQuery, HTML5 etc. He is the author of this blog.