制作多个文件以强制下载 [英] Make multiple files to force-download

查看:52
本文介绍了制作多个文件以强制下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,标题没有解释太多.让我试着进一步解释一下.

Sorry if the title doesn't explain much. Let me try to explain further.

我有这样的代码:

<?
//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";
}
?>

$array 是一个数据数组.

With $array being an array of data.

现在有时数据量可能会超过5000条,所以如果数据超过5000条,每回显5000条数据再做一个文件.Ea:如果$array中有20000条数据,那么一共是4个文件.

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.

推荐答案

这里介绍如何下载多个文件...技巧是在同一页面中呈现多个 iframe.每一帧都有不同的来源,强制下载不同的文件

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

<?php
for($i = 0; $i<5; $i++){
    echo '<iframe src="test_multiple_downfile.php?text='.$i.'">/iframe>';
}
?>

test_multiple_downfile.php 内容是这样的:

test_multiple_downfile.php content is this:

$out = $_GET['text'];
header("Content-Type: plain/text");
header("Content-Disposition: Attachment; filename=testfile_".$out.".txt");
header("Pragma: no-cache");
echo "$out";

这篇关于制作多个文件以强制下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆