Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-11-2009, 02:19 PM   PM User | #1
houssam_ballout
New Coder

 
Join Date: Apr 2006
Location: Lebanon
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
houssam_ballout is an unknown quantity at this point
2 files php download

Hi,
how can I download 2 different files simultaneously using php?
so if I can click on a button or link it will download the 2 files.

Any help
Thanks
houssam_ballout is offline   Reply With Quote
Old 03-11-2009, 06:08 PM   PM User | #2
cmancone
New Coder

 
Join Date: Mar 2009
Posts: 52
Thanks: 4
Thanked 6 Times in 6 Posts
cmancone is an unknown quantity at this point
Have the link be a javascript function which creates two popup windows, each of which points to a different file to download. As far as I know you can't simultaneously download two different files. I suppose you could use php to zip both files and then send the zipped file for download, though.
cmancone is offline   Reply With Quote
Old 03-12-2009, 05:07 AM   PM User | #3
milanchheda
New Coder

 
Join Date: Feb 2009
Posts: 38
Thanks: 0
Thanked 1 Time in 1 Post
milanchheda is an unknown quantity at this point
You can make a zip of those files and force the user to download the zip.
Below is the function to perform the same.
PHP Code:
//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_nameZIPARCHIVE::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;

PHP Code:
 $file_names=array('test.php','test1.txt');
  
$archive_file_name='zipped.zip';
  
$file_path=dirname(__FILE__).'/';
  
zipFilesAndDownload($file_names,$archive_file_name,$file_path); 
Ιn thе аbove ΡHP function, an object of ZipArchive ϲlass is used. This library іs bundled іn ΡHP аfter thе version of ΡHP 5.2 onlу.So, if уou’rе uѕing thе ΡHP version oldеr thаn thаt onе thеn уou’vе to gеt іt from ΡECL extension.

Last edited by milanchheda; 03-12-2009 at 05:10 AM..
milanchheda is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:53 AM.


Advertisement
Log in to turn off these ads.