PHP:在飞行中压缩远程文件

时间:2015-02-13 18:34:51

标签: php file stream download zip

我想在下载时动态创建一个Zip文件。 让我解释: 我有一些来自远程服务器的网址(每次更改),我想为所有人创建一个容器(zip存档),这样就可以立即下载它们,而不是单独下载所有文件。

这是我的代码:

$files = array(
    'http://example.com/file1.pdf'=> array('size'=>55050),
    'http://example.com/file2.doc'=> array('size'=>10000),
    'http://example.com/file3.txt'=> array('size'=>5000)
);
$total_size = 0;
foreach($files as $file=>$info){
    $total_size = $total_size+(int)$info['size'];
}

ob_start();
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="Test.zip"');
header('Content-Length: '.$total_size);
header("Content-Transfer-Encoding: binary");
header('Content-Encoding: chunked');

foreach($files as $url=>$info){
    while (@ob_end_flush());
    readfile($url);
}

ob_end_flush();
ob_end_clean();
exit;

这会(显然)生成一个损坏的档案...... 谁能帮我? 提前谢谢。

0 个答案:

没有答案
相关问题