Solved

Download header cause zip file to be corrupt

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

751 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