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.

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

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题