1

我一直在根据问题在我的网站上测试以下代码: 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);

它可以很好地动态传输拉链。

但是有一个问题是它不会将文件大小发送给用户。大概是因为它以动态压缩的形式发送数据。

由于我使用的是零压缩压缩,有什么方法可以让它将大小发送给用户,因为大小是已知的?谢谢

4

1 回答 1

0

您必须发送一个内容长度标头,如下所示:

header("Content-Length: $filesize");
于 2012-08-08T14:24:41.663 回答