Go Back   Cloud Computing > Support > PHP Forum
 

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 10-11-11, 02:24
BOD Member
 
Join Date: Aug 2011
Posts: 76
Default multiple file download in php

Hi,
I know the way to download a single file using php but...i need something which works for multiple downloads....i mean two or more files should get downloaded from a single page...is it possible?
regards
Reply With Quote
  #2 (permalink)  
Old 10-11-11, 08:22
BOD Member
 
Join Date: Apr 2011
Posts: 74
Default

You should use the ZipArchive class to make a ZIP file and stream it to the client. Something like follows:

Quote:
PHP function to download multiple 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;
}
In the above PHP function (object of ZipArchive class). Note that this library is packaged in PHP after the version of PHP 5.2. If you are using the older PHP version, then you have to get it from PECL extension.

Example of Using Above PHP function

Quote:
*$file_names=array('test.php','test1.txt');
* $archive_file_name='zipped.zip';
* $file_path=dirname(__FILE__).'/';
* zipFilesAndDownload($file_names,$archive_file_name ,$file_path);
After calling above function you will get the zip archive containing multiple files passed as array in the first parameter of the function.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump


All times are GMT -6. The time now is 01:19.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright © 1999-2012, BODHost Ltd. All rights reserved.