Hello guyz,
I need some help about downloading MULTIPLE files on php. I've used while loop by checking if the checkboxes were checked but when I continued on clicking the download button, all the system downloads is just 1 file. Why is that?.. any suggestions would be appreciated. Thanks.
I've provided the codes for easier tracing (I've included two files----Upload(w/c is working fine) and the download)
<--uploadserver.php (file 1)-->
<?php
echo $reload=$_SERVER['PHP_SELF'];
?>
<form action="<?$reload?>" method="post" name="fileForm" enctype="multipart/form-data">
File to upload:
<table>
<tr><td><input name="upfile" type="file"></td></tr>
<tr><td><input type="submit" name="submitBtn" value="Upload"></td></tr>
</table>
</form>
<?php
if(isset($_POST['submitBtn']))
{
if(file_exists("d:/Upload")==0)
echo mkdir("d:/Upload");
$target_path="d:/Upload/";
$x="d";
$file_name=$_FILES['upfile']['name'];
if(file_exists("d:/Upload/$file_name")==0)
{
echo '<br>Target Path: '.$target_path=$target_path.basename($_FILES['upfile']['name']).'';
if(move_uploaded_file($_FILES['upfile']['tmp_name'], $target_path))
{
echo "<br>File:". basename($_FILES['upfile']['name']).' uploaded...';
echo "<br>Working directory :".getcwd();
}
else
echo error;
}
else
echo "<br>Error: File duplication";
}
?>
<a href="downloadserver.php"><br>Download</a>
<--downloadserver.php (file 2)-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
<meta name="created" content="Sat, 17 Dec 2011 01:37:35 GMT">
<meta name="description" content="">
<meta name="keywords" content="">
<title></title>
<!--[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<?php
$reload=$_SERVER['PHP_SELF'];
?>
<form name='download' method='post' name='download' action="<?php $reload?>">
<?php
$dir="d:Upload/";
if(is_dir($dir))
{
if($folder=opendir($dir))
{
echo 'Current files: ';
while(($file=readdir($folder))!==false)
{
if(filetype($dir.$file)!='dir')
echo "<br>  <input type='checkbox' name='boxes[]' value='$file'>'$file'</input>";
}
closedir($folder);
}
session_start();
$_SESSION['dir']=$dir;
}
?>
<br>
<input type="submit" name="submit" value="Download selected file/s"/>
</form>
<?php
if(isset($_POST['submit']))
{
$dir=$_SESSION['dir'];
$index=0;
echo 'Total count: '.count($_POST['boxes']).'';
while($index<count($_POST['boxes']))
{
if(isset($_POST['boxes'][$index]))
{
$file=$_POST['boxes'][$index];
$path="$dir$file";
echo "<br>$path";
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($path));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);
}
else
echo "<br>Index: $index not set";
$index++;
}
}
?>
</body>
</html>