In this article am going to explain, how to create a Gmail like
multiple file download by creating a zip file in PHP. The files should
be in a web directory and we are creating a zip file using PHP and force
it to download. You can add any types of file such as image, mp3, pdf
movie files or even rar files also. This system is also known as Multiple File Download In PHP

You can flush the file name from database and files will be in
web directory or you have to pass the file name in to the array. In this
example I will show you the both.If you ate fetching the file names from database means use below method
//if you are getting the file name from database means use the following method //Include DB connection require_once('db.php'); //Mysql query to fetch file names $cqurfetch=mysql_query("select * from files"); //create an empty array $file_names = array(); //fetch the names from database while($row = mysql_fetch_array($cqurfetch, MYSQL_NUM)) { //Add the values to the array //Below 8 ,eams the the number of the mysql table column $file_names[] = $row[8]; }Or you can pass the values to the array directly like below:
- code
- source
$file_names = array(‘file1.ext’,’file2.ext’,’file3.ext’);
Find the full final script below
- code
- source
//This script is developed by www.webinfopedia.com
//For more examples in php visit www.webinfopedia.com
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
$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);
//echo $file_path.$files,$files."<br>";
}
$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;
}
//If you are passing the file names to thae array directly use the following method
$file_names = array('makeZipinPHP.jpg','Speed_Kills.wav','tick.jpg','webinfopedia.com.txt');
//if you are getting the file name from database means use the following method
//Include DB connection
require_once('db.php');
//Mysql query to fetch file names
$cqurfetch=mysql_query("select * from files");
//create an empty array
$file_names = array();
//fetch the names from database
while($row = mysql_fetch_array($cqurfetch, MYSQL_NUM))
{
//Add the values to the array
//Below 8 ,eams the the number of the mysql table column
$file_names[] = $row[8];
}
//Archive name
$archive_file_name=$name.'DEMOphpCreateZipTodownloadMultipleFiles.zip';
//Download Files path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/images/';
//cal the function
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
You can customize this code according to your need. Hope that it
will help you. You can see the same like this example in Gmail multiple
file download.
No comments:
Post a Comment