Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Create Zip file in PHP, Force Download allowing Download Managers

Posted on 2008-10-14
3
Medium Priority
?
820 Views
Last Modified: 2013-12-25
Hi,

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?)


<?
	session_start();
	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_register('dl_filename');
		$_SESSION['dl_filename'] = $zipname;
		$zipfile = New Archive_Zip($zipname);
		$zipfile->create($filearray);	
	}
 
 
	header("Content-Type: application/zip ");
	header("Content-Disposition: attachment; filename=".$zipname);
	readfile($zipname); 
?>

Open in new window

0
Comment
Question by:djdiablo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
hampus_b earned 300 total points
ID: 22710262
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:
site.com/files/a.doc_b.doc_c.doc.zip

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

Author Closing Comment

by:djdiablo
ID: 31505831
Thanks, a good common sense answer which I hadn't thought of!
0
 
LVL 1

Author Comment

by:djdiablo
ID: 22713686
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);
		$zipfile->create($filearray);	
	}
 
	header("Content-Type: application/zip ");
	header("Content-Disposition: attachment; filename=".$zipname);
	header("Content-Length: ".filesize($zipname));
	readfile("$zipname"); 

Open in new window

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

650 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