1

I have been testing the following code on my site from the question: LAMP: How to create .Zip of large files for the user on the fly, without disk/CPU thrashing

<?php
// make sure to send all headers first
// Content-Type is the most important one (probably)
//
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="file.zip"');

// use popen to execute a unix command pipeline
// and grab the stdout as a php stream
// (you can use proc_open instead if you need to 
// control the input of the pipeline too)
//
$fp = popen('zip -0 -r - file1 file2 file3', 'r');

// pick a bufsize that makes you happy (8192 has been suggested).
$bufsize = 8192;
$buff = '';
while( !feof($fp) ) {
   $buff = fread($fp, $bufsize);
   echo $buff;
   flush();
}
pclose($fp);

It works well to stream the zipping on the fly.

But there is a problem that it does not send the file size to the user. Presumably because its sends the data as its zipping on the fly.

Since I am using zero compression zipping, is there any way to have it send the size to the user since the size is known? Thanks

Community
  • 1
  • 1

1 Answers1

0

You have to send a content-length header, like this:

header("Content-Length: $filesize");
periklis
  • 9,667
  • 6
  • 55
  • 63
  • How would you add this header automatically when the site generates the download? – user1580294 Aug 08 '12 at 17:07
  • Just like you set the other 2 headers – periklis Aug 08 '12 at 19:07
  • 1
    The problem is that I do not have the final file size at the start of the download since the file is being zipped on the fly. Is it possible to use an estimate of the file size in the header? – user1580294 Aug 10 '12 at 17:33
  • That's a good question and I don't know the answer (but my guess is that you can use an estimate). Perhaps you can try sending first a file that you know its size and see if toggling the content-length messes with it – periklis Aug 10 '12 at 19:19