Create Zip file in PHP, Force Download allowing Download Managers

Posted on 2008-10-14
Last Modified: 2013-12-25

I'm looking to create a zip file on the fly and download it immediately using PHP. The purpose is to allow users to select a number of files on our server and download them in a tidy zip. I'm having problems with IE7 and Getright however.

Basically, I want to create a unique filename for the zip, store it on the server and force the user to download it. Ideally then I will also delete the file to prevent the server clogging up. Firefox is fine with this code however if you combine a download manager, it creates two (sometimes three!) unique files. I can only assume that the sessions do not transfer to the download manager so it creates a new file.

I've attached some code as shown below where I've tried to use sessions to get around the problem.

(I'm using the pear/zip files from another project - there may be a better way?)



	include ('pear/Zip.php');

	// For testing only (we have database queries etc here)

	$filearray = array("file1.jpg,file2.jpg,file3.jpg"); 

	if (isset($_SESSION['dl_filename'])) {

		$zipname = $_SESSION['dl_filename'];

	} else {

		$zipname = "masszip_".rand(0,99999).".zip";


		$_SESSION['dl_filename'] = $zipname;

		$zipfile = New Archive_Zip($zipname);



	header("Content-Type: application/zip ");

	header("Content-Disposition: attachment; filename=".$zipname);



Open in new window

Question by:djdiablo
  • 2

Accepted Solution

hampus_b earned 100 total points
Comment Utility
why not create a not so unique zip? for example, concat the names of the files and make a hash from the names. then the request for the same files would ask for the same file, and you could leave it on the server.

Or, with mod rewrite in apache, point *.zip files to a php-script, and let php create the zip on the fly like this:

The script can then serve the files on the fly or cache them on the server.

Author Closing Comment

Comment Utility
Thanks, a good common sense answer which I hadn't thought of!

Author Comment

Comment Utility
Using the above solution, I created the following code. It now works as required without the need for sessions. Thanks for your help!
	// Create MD5

	$md5files = md5(implode("",$filearray));

	$md5n = strtolower(substr($md5files,0,7));


	// Add to Filename

	$zipname = "masszip_".$md5n.".zip";


	if (file_exists($zipname)) {

		$already = 1;

	} else {

		$zipfile = New Archive_Zip($zipname);



	header("Content-Type: application/zip ");

	header("Content-Disposition: attachment; filename=".$zipname);

	header("Content-Length: ".filesize($zipname));


Open in new window


Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now