douxun4924 2014-11-27 23:51
浏览 57
已采纳

在PHP中将多个CSV文件输出到浏览器

I have a large large table of data (in mongodb) with more than 5 million rows. I want to export the data to csv and output the csv in browser directly. I am using PHP.

For smaller record set less than a million rows, the following code works perfectly.

set_time_limit(0);
$output = fopen("php://output",'w') or die("Can't open php://output");
header("Content-Type:application/csv"); 
header("Content-Disposition:attachment;filename=auditresults_$i.csv"); 
fputcsv($output, array('id','createdon','colum1','column2','column3','colum4'));                             
foreach ($items as $item) {               
     fputcsv($output, $item)                 
}            
fclose($output) or die("Can't close php://output");    

However, when I export large data sets such as 5 million rows, the above code fails.

I want to see if there is a way to split the above results in batches of 100,000 rows and output multiple csv files through the browser.

  • 写回答

1条回答 默认 最新

  • duan62819774 2014-11-28 00:03
    关注

    You can only send a single response to a request.

    The solution could be to output multiple csv files in a temporary folder on your website, and send the downloadable url links to those files as a response.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档