Force download multiple files in a zip archive using PHP

In this post, I’ll show you how can you download the multiples files in a zip archive using PHP. I’ve made a function in PHP where you’ve to pass the parameters the array of files to be zipped, the second parameter is file name as which zip archive file has to be downloaded and finally the path of files where files to be zipped are located.(assuming that they are all in same folder)

PHP function to download mutiple files in a zip archive

//function to zip and force download the files using PHP
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
  //create the object
  $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)
  {
    $zip->addFile($file_path.$files,$files);
  }
  $zip->close();

  //then send the headers to foce download the zip file
  header("Content-type: application/zip");
  header("Content-Disposition: attachment; filename=$archive_file_name");
  header("Pragma: no-cache");
  header("Expires: 0");
  readfile("$archive_file_name");
  exit;
}

Download Source code

In the above PHP function, first of all the object of ZipArchive class. Remember that this library is bundled in PHP after the version of PHP 5.2 only.If you’re using the PHP version older than that one then you’ve to get it from PECL extension.

After that, we’ve tried to create the zip arhive with the open() function using the ZIPARCHIVE::CREATE flag.After successfully creating the archive, each files whose names are passed as array are added to zip file using addFile() function of ZipArchive() class.Then, this zip archive is closed using close() function of same class.

And, finally different headers are passed through PHP to force download the newly created zip file.

Example of Using Above PHP function

  $file_names=array('test.php','test1.txt');
  $archive_file_name='zipped.zip';
  $file_path=dirname(__FILE__).'/';
  zipFilesAndDownload($file_names,$archive_file_name,$file_path);

The above PHP function call is straighforward and after calling that function you’ll get the zip archive containing mutiple files passed as array in the first parameter of the function.

Popularity: 9%

Tags: ,

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Related Posts

» Prevent Directory Listing using .htaccess
» Uploading large(big) files in PHP using .htaccess
» Absolute path and Relative path file inclusion in PHP
» Blogger Sucks, Wordpress Wowww!!

23 Comments on “Force download multiple files in a zip archive using PHP”

  • Felix wrote on 2 September, 2008, 17:43

    Usefull!!
    tks Roshan to share this code.

  • SNaRe wrote on 2 September, 2008, 17:54

    Firstly thank you for share. But I have a question. I wrote a smilar script for myself.
    It’s also using readfile function.
    I’m using external links to download . They are not on my host.
    So when i want use readfile functions will it use my websites bandwith (because of readfile) or will it use from external website.
    I hope i could explain

  • Roshan wrote on 3 September, 2008, 6:01

    @SNaRe - You can see the description from php.net
    Reads a file and writes it to the output buffer.

    Which clearly tells that it reads the file from external source and write it to the output butter which is gonna consume the bandwidth of both websites….

  • Tarquin wrote on 4 September, 2008, 6:40

    Very useful for community sites, useful with a checkox list, just gotta know how to intergrate it =P

    Thanks again :)

  • Christoph wrote on 6 September, 2008, 6:19

    Great,thx for sharing! Would be great with a post doing the reversal thing, extracting files from an uploaded zip-file. Challenge, Roshan ;)

    Best regards, pal!

    C.

  • Roshan wrote on 6 September, 2008, 15:50

    Christoph……thanks for the the idea…I’ve already thought for that and you’ll be getting that post soon…

  • KevinK wrote on 7 September, 2008, 3:40

    The example creates a .zip on the server which can be opened. But the downloaded file is a slightly different size and Winzip says it is corrupt. Is there some additional header magic?

  • Roshan wrote on 7 September, 2008, 4:33

    @kevinK- I had the same problem of corrupted archive. It is the but of the zip library upgrade your PHP version to latest or upgrade the library file from CVS.

    http://bugs.php.net/bug.php?id=39506

  • Muhammad Ali wrote on 10 September, 2008, 9:14

    Oh thats nice, i was in search of such a code that can generate dynamically the .zip file, i got it here, thanks for sharing

  • KevinK wrote on 10 September, 2008, 14:51

    Again, the zip created on the server is OK. It can be opened. It is after downloading to the client that the zip can’t be opened.

    The server and client versions are the same size. What could be wrong with this code?

    function download_zip($filename) {
    ini_set(”zlib.output_compression”, “Off”);
    header(”Cache-Control: must-revalidate, post-check=0, pre-check=0″);
    header(”Cache-Control: private”);
    header(’Pragma: no-cache’);
    header(’Expires: 0′);
    header(”Content-Description: File Transfer”);
    header(’Content-disposition: attachment; filename=’.basename($filename));
    header(”Content-Type: application/zip”);
    header(”Accept-Ranges:bytes”);
    header(”Content-Length:”.filesize($filename));
    $fp = @fopen($filename,”rb”);
    fpassthru($fp);
    fclose($fp);

    ob_flush();
    flush();
    }

  • Roshan wrote on 10 September, 2008, 15:23

    @KevinK- Why don’t you pass the headers as I’ve posted above, it is working fine…

  • diego wrote on 11 September, 2008, 3:53

    hey, thanks for sharing ….
    very nice script, im using it

  • W. H. Evans wrote on 12 September, 2008, 11:30

    Thanks. The script saved me some time.

  • danish wrote on 26 September, 2008, 10:28

    Excellent script … thanks for the script man ..

  • bharat wrote on 3 October, 2008, 13:38

    Hi, roshan

    I have a large number of files (about 2000 html files) which is inside a folder and i want to download it by using this function , is it possible?

    but i think that creating an array for all these 2000 html files for first argument of these function is hard work.

    can you provide some solution

  • Roshan wrote on 3 October, 2008, 15:24

    @bharat- to simplify this you can use opendir() and array_push() function to add these files to array……

  • Pranjal wrote on 10 November, 2008, 21:51

    Is there a way to create a form that has check boxes for downloading files. By this I mean, if I want to download file 1, 2, 5, and 7, I can check the 1,2,5, and 7 checkboxes, hit submit, and they would all download to my computer in a zip format from a remote or local server.

  • Roshan wrote on 11 November, 2008, 10:09

    @pranjal - you can do it by using the checkbox and use array_push() function to store the file name in the array when posted…….simple….

  • pranjal wrote on 11 November, 2008, 23:21

    Thanks, for the reply Roshan.Checkboxes are working.Now I am having one more problem. The script creates a zip folder also on the server for each download. Is there a way that I can differentiate between the two zip files. one which the user is downloading and other which is getting saved on the server. So that I can put the server zip file in a different folder.

  • maleos wrote on 4 December, 2008, 8:54

    nice script. i wonder i you have the deletion script as well. cron job running script.

  • Luke wrote on 19 December, 2008, 15:20

    could this work with files that are stored in a database as blobs?

  • Roshan wrote on 20 December, 2008, 15:04

    @Luke - I never store the files in database as a blob as it increases the database overhead….so haven’t tested with blob data…

  • Smitha wrote on 30 December, 2008, 23:48

    I would like to use this function to download zip files, but I am using PHP 5.2. Could you please let me know how to get the ZipArchive class from PECL extension.
    Thanks,
    Smitha

Write a Comment

 


Copyright © 2009 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.