Solved

Download header cause zip file to be corrupt

Posted on 2008-10-23
4
1,183 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
Comment Utility
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:
Squinky earned 500 total points
Comment Utility
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
Comment Utility
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:Squinky
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 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

12 Experts available now in Live!

Get 1:1 Help Now