我有以下代码可以通过代码下载一些日志文件
$files = array( '../tmp/logs/debug.log',
'../tmp/logs/error.log');
foreach($files as $file) {
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: text/html");
header("Content-Transfer-Encoding: binary");
// read the file from disk
readfile($file);
}
但是只下载数组的第一个元素。在本例中,debug.log,如果我交换元素,那么只有error.log。有什么帮助吗?
发布于 2016-04-27 01:51:19
每个HTTP请求只能下载一个文件。实际上,一旦发送了第一个文件,浏览器就会假设这是处理的结束,并停止与服务器的对话。
如果您想确保用户下载多个文件,一种解决方案可能是在服务器端快速将它们全部压缩,然后将zip文件发送给用户下载。
发布于 2016-04-27 02:15:46
不能一次下载多个文件。HTTP协议设计为每个请求发送一个文件。
或者,您可以压缩所有日志文件并将其作为zip文件下载。
您可以使用ZipArchive
类创建一个ZIP文件并将其流到客户端。类似于:
$files = array(
'../tmp/logs/debug.log',
'../tmp/logs/error.log'
);
$zipname = 'logs.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
并使之流淌:
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
发布于 2016-04-27 01:45:23
标头在相同的执行中设置一次。如果放入一个循环,下一个标头将不会发送。您可以使用javascript进行循环并使用ajax进行调用,但是用户将同时获得多个下载,因此它可以使浏览器崩溃和可用性崩溃。
https://stackoverflow.com/questions/36885750
复制相似问题