Solved

Download header cause zip file to be corrupt

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
if (is_singular not working 5 37
php error 27 55
warning code: class php incomplete message 6 27
How do I speed up this PDO query 4 12
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

735 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