Laravel导出百万数据
$time = time();$dir_path = public_path('shiporder');if (!is_dir($dir_path))mkdir($dir_path, 0777, true);// 文件名$fileName = date('Ymd') . '.csv';//检测文件是否已存在if (file_exists($dir_path . '/'..
·
$time = time();
$dir_path = public_path('orderlog');
if (!is_dir($dir_path))
mkdir($dir_path, 0777, true);
// 文件名
$fileName = date('Ymd') . '.csv';
// 检测文件是否已存在
if (file_exists($dir_path . '/' . $fileName))
unlink($dir_path . '/' . $fileName);
// 打开文件
$myfile = fopen($dir_path . '/' . $fileName, "w") or die("Unable to open file!");
$title = ['日志ID', '订单号', '用户名', '标题', '内容', '添加时间'];
$head = mb_convert_encoding($title, 'GBK', 'utf-8');
fwrite($myfile, $head);
$rows = DB::table('order_log');
$this->output->progressStart($rows->count());
$rows->chunkById(1000, function($items) use (&$myfile) {
foreach ($items as $item) {
$this->output->progressAdvance();
$data = [
$item->log_id,$item->order_md5,$item->username,$item->title,$item->content,date('Y-m-d', $item->add_time)
];
fputcsv($myfile, $data);
}
}, 'log_id');
$this->output->progressFinish();
$endTime = time();
$t = round(($endTime - $time) / 60);
$this->info("共耗时:".$t."分钟");

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)