Link to home
Start Free TrialLog in
Avatar of CalmSoul
CalmSoulFlag for United States of America

asked on

Download header cause zip file to be corrupt

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

Avatar of Xyptilon2
Xyptilon2
Flag of China image

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));
?>
ASKER CERTIFIED SOLUTION
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
}
?>
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.