//控制器代码
$file_name = $filePrefix . date('Y_m_d_H_i_s') . ".csv";
csv($file_name, [
'标题1',
'标题2'
], $dial_log);//文件名,文件标题内容,数据列表
//公共文件调用
//php导出csv并保存在服务器,返回csv的文件路径
function csv($file_name, $title, $list){
//dump($_SERVER['HTTP_HOST']);
$file_path= $_SERVER['DOCUMENT_ROOT'] . "/static/uploads/csv/" . $file_name;
//dump($file_path);
if (file_exists($file_path)) {
unlink($file_path);
}
$file_path= $_SERVER['DOCUMENT_ROOT'] . "/static/uploads/csv/" . $file_name;
$fp = fopen($file_path, "a");
$limit = 1000;
$calc = 0;
foreach ($title as $v) {
$tit[] = iconv('UTF-8', 'GB2312//IGNORE', $v);
}
fputcsv($fp, $tit);
foreach ($list as $v) {
$calc ++;
if ($limit == $calc) {
ob_flush();
flush();
$calc = 0;
}
foreach ($v as $_v) {
$tarr[] = iconv("UTF-8", "GB2312//IGNORE", $_v);
//$tarr[] = $_v;
}
fputcsv($fp, $tarr);
unset($tarr);
}
unset($dial_log);
fclose($fp);
//exit();
}