?
Solved

Problem with a PHP auto-download of a ZIP file

Posted on 2009-02-19
7
Medium Priority
?
715 Views
Last Modified: 2012-05-06
I have written a PHP script that automatically begins the download of a ZIP file when a user goes to the page. I created the ZIP file with XP's built-in archiver (Right-click the files, Send To -> Compressed Folder). This ZIP file extracts fine on my PC, and it extracts fine if the users access it by going directly to its URL (i.e., http://www.mysite.com/file.zip).

However, if they go to the PHP script that launches the automatic download (i.e., http://www.mysite.com/download.php), they can't open the ZIP file. When they try to extract the ZIP, they get an error message that says:

"Compressed (zipped) Folders Error

Windows cannot open the folder.

The Compressed (zipped) Folder: 'C:\Downloads\file.zip' is invalid."


On Windows XP, it's a similar error message that says the compressed folder is invalid or corrupted. Any ideas what the problem is? It's strange how it works fine if they download the file directly.
<?
	
	session_start();
	
	$fpath = 'cdl/client.zip';
	
	if( $_SESSION['psys']['logged'] == true )
	{
		header( 'Pragma: public' );
		header( 'Expires: 0' );
		header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
		header( 'Cache-Control: private', false );
		header( 'Content-Type: application/zip' );
		header( 'Content-Disposition: attachment; filename="' . basename( $fpath ) . '";' );
		header( 'Content-Transfer-Encoding: binary' );
		header( 'Content-Length: ' . filesize( $fpath ) );
		
		readfile( $fpath );
		exit();
	}
	else
	{
		header( 'Location: index.php' );
	}
	
?>

Open in new window

0
Comment
Question by:elorc
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 10

Expert Comment

by:aboo_s
ID: 23682172
try this:
'Content-Type: application/octet-stream'
instead of this:
'Content-Type: application/zip'
0
 
LVL 8

Expert Comment

by:agamal
ID: 23682395
your code worked fien with me ..... and the file is valied
0
 
LVL 1

Author Comment

by:elorc
ID: 23685388
I tried changing it to application/octet-stream but I'm observing the same behavior, unfortunately. It still will only unzip if the file is downloaded directly. Retrieving it with the auto-downloading script results in the "Compressed (zipped) folder error" message.
<?
	
	session_start();
	
	$fpath = 'cdl/client.zip';
	
	if( $_SESSION['psys']['logged'] == true )
	{
		header( 'Pragma: public' );
		header( 'Expires: 0' );
		header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
		header( 'Cache-Control: private', false );
		//header( 'Content-Type: application/zip' );
		header( 'Content-Type: application/octet-stream' );
		header( 'Content-Disposition: attachment; filename="' . basename( $fpath ) . '";' );
		header( 'Content-Transfer-Encoding: binary' );
		header( 'Content-Length: ' . filesize( $fpath ) );
		
		readfile( $fpath );
		exit();
	}
	else
	{
		header( 'Location: index.php' );
	}
	
?>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Accepted Solution

by:
agamal earned 1000 total points
ID: 23686031
try using only the following 3 lines as your header lines


header("Content-Transfer-Encoding: binary");
header("Content-Type: application/octet-stream");
header( 'Content-Disposition: attachment; filename="' . basename( $fpath ) . '";' );

0
 
LVL 10

Expert Comment

by:aboo_s
ID: 23689954
This really sounds bizzar, but what I have learned is bizzar probs need bizzar solutions.

Please try this:
header( 'Content-Disposition: attachment; filename=' . basename( $fpath ) . ';' );
instead of this:
header( 'Content-Disposition: attachment; filename="' . basename( $fpath ) . '";' );
tell me if it worked!
0
 
LVL 1

Author Closing Comment

by:elorc
ID: 31548765
That appears to have done the trick. I'm curious if it was the one line where I specified the length of the file... maybe that was screwing it up. Anyhow, it works with just those three lines so I'm leaving it like that for now. :)

Thanks!
0
 

Expert Comment

by:tonikgroup
ID: 24236892
Doesn't the browser need the following:

 header( 'Content-Length: ' . filesize( $fpath ) );

to estimate the remaining download time/size?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

809 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