php 制作多个文件以强制下载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16390601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make multiple files to force-download
提问by user1687621
Sorry if the title doesn't explain much. Let me try to explain further.
对不起,如果标题没有解释太多。让我试着进一步解释一下。
I have code that looks like this:
我有如下代码:
<?
//Grab the number in count.txt
$count = intval(file_get_contents('count.txt'));
//Move the number up one
file_put_contents('count.txt', ++$count);
$number = file_get_contents('count.txt');
//Force Download
header('Content-Disposition: attachment; filename=DataFiles'.$number.".csv");
header('Content-Type: application/octet-stream');
//The data
foreach($array as $info){
echo $info."\n";
}
?>
With $array being an array of data.
$array 是一个数据数组。
Now sometimes the amount of data can be more than 5000, so if the data is over 5000, make another file for every 5000 data that is being echoed. Ea: If there is 20,000 pieces of data in the $array, then it will make a total of 4 files.
现在有时候数据量会超过5000条,所以如果数据超过5000条,每回显的5000条数据再做一个文件。Ea:如果$array中有20,000条数据,那么一共是4个文件。
回答by sergserg
You cannot send more than 1 file in response to an HTTP request.
您不能发送超过 1 个文件来响应 HTTP 请求。
What I would suggest is zip the file in a single file and return that.
我的建议是将文件压缩到一个文件中并返回。
回答by fuxas
Here you are how to download multiple files... The tricks is rendering many iframe in same page. Every frame has different source that force download different files
这是下载多个文件的方法...诀窍是在同一页面中渲染多个 iframe。每个帧都有不同的来源,强制下载不同的文件
<?php
for($i = 0; $i<5; $i++){
echo '<iframe src="test_multiple_downfile.php?text='.$i.'">/iframe>';
}
?>
test_multiple_downfile.php content is this:
test_multiple_downfile.php 内容是这样的:
$out = $_GET['text'];
header("Content-Type: plain/text");
header("Content-Disposition: Attachment; filename=testfile_".$out.".txt");
header("Pragma: no-cache");
echo "$out";
回答by Miky Dal
I use AJAX and jQuery to do it, one way to do it, is to prepare all your files and put them into the session where each files have a unique ID. The the return of the ajax call is a call to a JavaScript with the list of IDs.
我使用 AJAX 和 jQuery 来做到这一点,一种方法是准备所有文件并将它们放入会话中,其中每个文件都有一个唯一的 ID。ajax 调用的返回是对带有 ID 列表的 JavaScript 的调用。
The the JavaScript has to create as many hidden IFrame and you have IDs and in each of them, call a PHP to download the parts. That is where you put your download headers. Once you have finish the dump of the bytes, you delete them from the session. Using a unique ID and delete them after download makes sure nobody download twice the same file.
JavaScript 必须创建尽可能多的隐藏 IFrame 并且您有 ID,在每个 ID 中,调用 PHP 来下载部件。那是您放置下载标头的地方。完成字节的转储后,将它们从会话中删除。使用唯一 ID 并在下载后删除它们可确保没有人下载相同的文件两次。
What is nice about ti is that if you refresh the page, since the IFrames were dynamic, they don't try to download another time. And if there are an error, the user won't see them since they are hidden IFrames.
ti 的优点在于,如果您刷新页面,由于 IFrame 是动态的,它们不会再尝试下载。如果出现错误,用户将看不到它们,因为它们是隐藏的 IFrame。
Consequently, if there is a problem with the download, instead of sending the header for download, you can send JavaScript alert or call to top.FunctionName('message'); to send some kind of alert to the user.
因此,如果下载出现问题,您可以发送 JavaScript 警报或调用 top.FunctionName('message'); 而不是发送下载标头。向用户发送某种警报。
I was able to go up to 10 downloads at the same time. But the user will be prompt 10 times too. This is something you have to consider if you don't want to zip them.
我最多可以同时下载 10 次。但用户也会被提示 10 次。如果您不想压缩它们,这是您必须考虑的事情。
Hope it helps.
希望能帮助到你。
回答by xdazz
No, it's not possible, because the HTTP protocol was designed to send one file per one request.
不,这是不可能的,因为 HTTP 协议旨在为每个请求发送一个文件。