本篇文章介紹的內容是關於PHP匯出上萬條資料,有需要的朋友可以參考一下
php利用緩衝實現動態輸出(ob_flush)
下面以PHP匯出上萬條資料為例,上代碼:
// 匯出測試 function explode() { ob_end_clean(); header("Content-type:text/csv");header('Content-Disposition: attachment;filename=test_' . time() . '.csv');$fp = fopen('php://output', 'a');// 輸出Excel列頭資訊$head = array('平台', '名稱', '本金','時間');// foreach($head as $i => $v) {// // CSV的Excel支援GBK編碼,一定要轉換,否則亂碼// $head[$i] = iconv('utf-8', 'GB2312//IGNORE', $v);// }fputcsv($fp, $head);$limit = 1000;$count = 1;$sql = 'select * from assets limit 0,1000';$rows = Db::table('表名')->query($sql);while(count($rows)>0 && $count<21) {$offset = $count*$limit; $count++; foreach ($rows as $key => $row) { if($key%2000 == 0) { if(ob_get_level()>0){ ob_flush() ; } } $val = array($row['platform'],$row['name'],$row['principal'],$row['create_time']); // foreach($val as $k => $v) { // $val[$k] = iconv('utf-8', 'GB2312//IGNORE', $v); // } fputcsv($fp, $val); } $sql = "select * from assets limit {$offset},{$limit}";$rows = Db::table('表名')->query($sql);}}