PHP Download Multiple Files


PHP Download Multiple Files

Below is the code which grabs file names to download and passes those names into the url.

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="file1" value="file1.pdf"/>
<input type="submit" name="file2" value ="file2.pdf"/>
<input type="hidden" name="getfile" value =""/>
</form>

<?php if($_POST['file1']){
$filename=$_POST['file1'];
echo '<a href="mydownload_multi.php?filename='.$filename.'"><b>Download file!</b></a>';
}
else if($_POST['file2']){
$filename=$_POST['file2'];
echo '<a href="mydownload_multi.php?filename='.$filename.'"><b>Download file!</b></a>';
}
?>

The code below is the actual file which makes it possible to download the selected download.

$myfilename=$_GET['filename'];
$filepath = $_SERVER['DOCUMENT_ROOT']."/ABOOK/MISC/downloading/".$myfilename;
//echo $_SERVER['DOCUMENT_ROOT'];
if (file_exists($filepath)) {
//download application
   header("Content-Type: application/force-download");
   //the filename will be given below
   header("Content-Disposition:filename=\"$myfilename\"");
//open file in binary mode   
$my_file = fopen($filepath, 'rb');
//output data
   fpassthru($my_file);
//close file
   fclose($my_file);
}