Create a Zip File Using PHP and Download Multiple Files

Demo Download

Today i am sharing how to create a zip file of multiple files using PHP and download that zip file on click. For this purpose i will need a folder where all files are available which can be selected and downloaded. I will also create a single php page which contain the following scripts.


Create a simple PHP page and copy paste the below form.


Add the following script in footer of web page. Don’t forget to add jQuery library before this script.

As you can see above that when you click on select all, all checkboxes are checked or unchecked. You can see my tutorial about how to select deselect all checkboxes using jquery.


Copy paste the below PHP script in the top of web page. When someone submit form first we are checking if files array is set or not, then we are creating zip file and downloading that file.

Demo Download

If you found this tutorial helpful so share it with your friends, developer groups and leave your comment.

Facebook Official Page: All PHP Tricks

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.


  1. Hi, How do we achieve this without the tick boxes to select the files, and instead just download all the files in the folder?. Can we give custom path to the folder?

  2. Its working fine on Localhost but not on the server need your help.

    $error = “”; //error holder
    $staff_row = Get_Row(“tbl_staff”,”id”,$_GET[‘staff_id’]);

    //$post = $_POST;
    $file_folder = “uploads/documents/”; // folder to load files
    // Checking ZIP extension is available
    $zip = new ZipArchive(); // Load zip library
    $zip_name = time().”.zip”; // Zip name
    if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
    // Opening zip file to load files
    $error .= “* Sorry ZIP creation failed at this time”;

    $Sql = “select * from tbl_staff_documents where staff_id='”.$_GET[‘staff_id’].”‘ “;
    $Sql.= ” order by id DESC “;
    $query = @mysql_query($Sql);
    while($Rows = @mysql_fetch_assoc($query))
    // Checking files are selected
    $file = $Rows[‘documents’];
    $zip->addFile($file_folder.$file); // Adding files into zip
    // push to download the zip
    header(‘Content-type: application/zip’);
    header(‘Content-Disposition: attachment; filename=”‘.$zip_name.'”‘);
    // remove zip file is exists in temp path
    $error.= “* You dont have ZIP extension”;

  3. Is there a good detailed reference link or “How To” on how to get this ZipArchive library working on PHP7.0 ?

  4. Hi,
    I think there is a small error in the code:

    if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)

    should be:
    if($zip->open($zip_name, ZIPARCHIVE::CREATE)==TRUE)

  5. thanks for the great tutorial!

    But I have issue, it’s not extracting properly on mac version.

    Please help.

      1. Hi, I used this code, but getting error that ZipArchive not found, can you please provide this file.

  6. Hi,
    great tutorial, would it be also possible to make another submit button which would send this zip file as email?

  7. $(‘#submit’).prop(“disabled”, true);
    $(“#checkAll”).change(function () {
    $(“input:checkbox”).prop(‘checked’, $(this).prop(“checked”));
    $(‘#submit’).prop(“disabled”, false);
    if ($(‘.chk’).filter(‘:checked’).length < 1){

    $('input:checkbox').click(function() {
    if ($(this).is(':checked')) {
    $('#submit').prop("disabled", false);
    } else {
    if ($('.chk').filter(':checked').length < 1){

    ehat is libarary of this code ?

  8. Can i use this code in wordpress for downloading? Is working good on localhost, but when i try in wordpress, don’t zip anything… Pls Help

    1. WordPress is a CMS, it has its own framework so you will need to implement according to its framework, this tutorial is not focused on WP. You have to follow WP guidance to create zip and download.

  9. I am having trouble getting the download to work. I have tried it on different servers using php5.2, 5.6 and 7.0. The php5.2 is working well, but not the other php. Do you know if there are any deprecated items in your script for php7.0 which is the final server?

    1. Hi Jesse, well i have not tested this script on PHP 7.0 so i am not sure if it will work on it or not. But you can see that i am using here the following:
      extension_loaded() function and new ZipArchive() Load zip library, make sure these both are enabled on your PHP 7.0 Version.

  10. Just what I am looking for!
    And no javascript…yay

    However the php is being stripped from my page. Would that be a server setting? What version of php does the script need on the server?


Leave a Reply

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