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.

HTML

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

jQuery

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.

PHP

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.

26 Comments

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

  2. 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)

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

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

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

    ehat is libarary of this code ?

  5. 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.

  6. 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.

  7. 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?

    thanks

Leave a Reply

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