Google SEO | PHP | Mysql | Ajax | Jquery | Photoshop | Illustrator | Database | Web Design | Web Development | Sitemap |

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

Create zip and download multiple file using 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.

Download FileDemo

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:
$file_names = array(‘file1.ext’,’file2.ext’,’file3.ext’);
Find the full final script below
//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."
"; } $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.

Top Λ

Comments (16)

  • rskullz

    thanks very much, your code is works
    Saturday-Jan-2014 | 07:16:55 PM
  • siva

    Hi, Can I download amazon s3 files?
    Tuesday-Nov-2013 | 06:04:40 PM
  • M3HRAN

    hi admin
    Where is the db.php file?
    Friday-Aug-2013 | 02:42:01 AM
  • arminder

    hi admin , how to use this script without database ?
    Sunday-Jan-2013 | 07:13:30 PM
  • Admin

    Yes.. you can do this without sql database. Hard code the file name directly.. :)
    Friday-Jan-2013 | 08:44:48 AM
  • Alex P

    Can this be done without sql?
    Thursday-Jan-2013 | 02:06:08 AM
  • Bhuvanesh

    Dear Admin I am also getting an error please check the code
    Tuesday-Dec-2012 | 12:13:39 PM
  • Yoga

    i was try this code, can download zip file but when i open that file i found error message "the archive is either in unknown format or damaged". any body can help me? thanks.
    Wednesday-Dec-2012 | 05:46:02 PM
  • Competition

    readfile("$archive_file_name");
    //ADD UNLINK TO DELETE FILE AFTER DOWNLOAD
    unlink("$archive_file_name");
    exit;
    
    Thursday-Nov-2012 | 06:51:06 AM
  • Admin

    Dear Nimesh, Send your script to admin@webinfopedia.com. i will check and let you know. Thank You
    Saturday-Oct-2012 |
  • nimesh

    Hello i have insert png and jpg file but this is not working in my pc zip file is created but not properly.
    Friday-Oct-2012 |
  • rati

    That can be right
    Wednesday-Aug-2012 | 3:52:35 PM
  • Arpit

    sir i use on my site zubarg script for downloading mp3 file in this when one user download file. other user not able to download. please tell me how multiple user download at a time. if i use this script my problem solve.
    Wednesday-May-2012 | 3:48:35 PM
  • shiji

    its working but in jpg and gif files no preview is available (blank image ) . can u please give me the guidance
    Friday-Nov-2011 | 2:54:37 PM
  • hui

    Hi, I don't know why I can not download the source code, can you send the file to my email? thank you!
    Tuesday-Sep-2011 | 4:25:52 PM
  • Snow

    Touchdown! That's a really cool way of putting it!
    Sunday-Sep-2011 |

Post Comment

Name:
Email:
Divider