$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."分钟");
Laravel导出百万数据
于 2022-01-26 00:16:17 首次发布