Solved

Download header cause zip file to be corrupt

Posted on 2008-10-23
4
1,185 Views
Last Modified: 2012-05-05
We are making a download manager application ...

Download header cause zip file to be corrupt

Please see the code below
header("Content-type: $mimeType");
		    header("Content-Transfer-Encoding: Binary");
		    header("Content-length: ".$size);
		    header("Content-disposition: attachment; filename=\"".$a['name']."\"");
		    readfile("$pathside");

Open in new window

0
Comment
Question by:CalmSoul
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:Xyptilon2
ID: 22794001
Try this, it works for me:

<?PHP
$file = '/home/test/somefile.dat';

header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename=' . basename($file));
?>
0
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 500 total points
ID: 22794338
Don't use application/force-download - it's a non-existent mime type and doesn't work for the reasons you think, and removes the ability of the user to handle the file type correctly (the user is always right). The correct MIME type for a zip file is application/zip. If you are going to include a filename in your content-disposition header, make sure it's correctly quoted and escaped. You don't need a transfer encoding header if it's just binary. Overall, this should work:
<?php
header("Content-Type: application/zip");
header("Content-Length: ".$size);
header("Content-Disposition: attachment; filename=\"".addslashes(basename($pathside))."\"");
readfile("$pathside");
?>

Open in new window

0
 
LVL 13

Expert Comment

by:Xyptilon2
ID: 22794493
Here's a function for large files:

<?PHP

readfile_chunked($file);

function readfile_chunked($filename) {
 $chunksize = 1*(1024*1024); // how many bytes per chunk
 $buffer = '';
 $handle = fopen($filename, 'rb');
 if ($handle === false) {
   return false;
 }
 while (!feof($handle)) {

   // ***** Write output to client
   $buffer = fread($handle, $chunksize);
   print $buffer;
 }
 return fclose($handle);
}
?>
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 22794614
So log as you don't have output buffering turned on (or you can turn it off on the fly), you shouldn't need to do that. PHP is built with mmap support by default which means it doesn't read the file into memory in order to send it - it just streams from disk to network. mmap and sendfile are also supported by apache, and are normally used for delivering all static files.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

810 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