【问题标题】:How To Send Content Size When Using PHP To Do On The Fly Streaming Zipping使用 PHP 进行动态流式压缩时如何发送内容大小
【发布时间】:2012-08-04 16:26:27
【问题描述】:

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

动态流式传输压缩效果很好。

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

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

【问题讨论】:

    标签: php bash streaming zip lamp


    【解决方案1】:

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

    header("Content-Length: $filesize");
    

    【讨论】:

    • 网站生成下载时如何自动添加此标头?
    • 就像你设置其他 2 个标题一样
    • 问题是我在下载开始时没有最终的文件大小,因为文件是动态压缩的。是否可以在标头中使用文件大小的估计值?
    • 这是一个很好的问题,但我不知道答案(但我猜你可以使用估计值)。也许您可以尝试先发送一个您知道其大小的文件,然后查看切换内容长度是否与它混淆
    猜你喜欢
    • 2014-04-28
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    相关资源
    最近更新 更多