<?php
$filename="backup_".date('m/d/Y', time()).".csv";
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$q=mysqli_query($conn,"SELECT * FROM visitordb");
$file = fopen('php://output','w');
if(mysqli_num_rows($q)>0) {
while($res = $q->fetch_assoc()) {
$datas = $res["sno"].','.$res["UDID"].','.$res["taggnumber"].','.$res["name"].','.$res["designation"].','.$res["company"].','.$res["email"];
fputcsv($file, explode(',', $datas));
}
} else {
}
fclose($file);
}
在 ms excel 中查看时,上述代码在 .csv 文件的开头生成空行。.csv 文件还会在页面中生成任何 html 代码。