Solved

Download header cause zip file to be corrupt

Posted on 2008-10-23
4
1,197 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
[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
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

627 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