How to Download multiple files as Zip in PHP

At our intial stage of programing we face various problems one of such is To to make user download multiple files in just one click

In this Tutorial we are going to explain how to create multiple file download by creating a zip file in php. The files should be in a web directory and we are creating a zip file using PHP and click it to download.
We can add any type of files such as .png,.jpg ,pdf files etc.

So,lets start a jump over a new hurdle,

We can fetch the file name from database and files will be in web directory or we have to pass the file name in to the array. In this example we will show you the both.

[cc code]
//if we are getting the file name from database means we use the following method
//Include database file
//Mysql query to fetch file names
$result=mysql_query(“select * from phphurdles_files”);

//create an empty array
$file_names = array();
//fetch the names from database
while($row = mysql_fetch_array($result))
//Add the values to the array
//Below image is the field name
$file_names[] = $row[‘image’];

Or we can pass the values to the array directly like below:

[cc code]
$file_names = array(phphurdles1.jpg,phphurdles2.png,phphurdles3.docx);


Now we will create a function to add each files of file_name array to archieve and will send headers to download the zip file on a single click
[cc code]
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)

$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit(“cannot open <$archive_file_name>\n”);
//add each files of $file_name array to archive
foreach($file_names as $files)
//then send the headers to download the zip file
header(“Content-type: application/zip”);
header(“Content-Disposition: attachment; filename=$archive_file_name”);
header(“Pragma: no-cache”);
header(“Expires: 0”);

//Archive Name
[cc code]
$archive_file_name=’’; //archive_file_name is the file name with which the zip file is created

//Download File Path
[cc code]
$file_path=dirname(__FILE__).’/’; forward slash can follow the folder name in which files are
We will create to call function with multiple parameters file_names array,archive name,filepath
[cc code]

Download Multiple Files

Now we will check the parameters and we will call the function
[cc code]

if ((isset($_GET[‘file_names’])) && if (isset($_GET[‘archive_file_name’])) && if (isset($_GET[‘file_path’]))){

echo zipFilesAndDownload($_GET[‘file_names’],$_GET[‘archive_file_name’],$_GET[‘file_path’]);



we can change this code according to our need. Hope that it will help you.
So guys keep motivating youself because hurdles makes you more creative


  1. arvind said:

    Thank you deepak shah………… very usefull..

    February 10, 2017
  2. I always emailed this web site post page to all my contacts,
    for the reason that if like to read it next my friends will too.

    May 19, 2017
    • Deepak Shah said:

      thankyou. we feel lucky if we are helping you in any way

      May 21, 2017
  3. Darshan said:

    what does your database contain?

    July 23, 2017

Leave a Reply

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