function make_csv($select_arr,$headername){
ini_set('max_execution_time', 300);
ini_set('memory_limit', '2048M');
$queryResult = $this->db->select('id,username,sf,youname,sex,sfz,tel,regtime,status,beizhu')->get_where('user','id in('.$idnum.')')->result_array();
$fileName = date("Ymd_his_").time() . '.csv';
$filePath = '/main/temp/csv/' . $fileName;
$index = 0;
$fp = fopen($filePath, 'w');
chmod($filePath, 0755);
$header = $headername;
fputcsv($fp, $header);
foreach ($queryResult as $key => &$val) {
foreach ($val as $k => $v) {
$val[$k] = $v . "\t";
if ($index == 10000) {
$index = 0;
ob_flush();
flush();
}
$index++;
}
fputcsv($fp, $val);
}
ob_flush();
fclose($fp);
header("Cache-Control: max-age=0");
header("Content-type:application/vnd.ms-excel;charset=UTF-8");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename=' . basename($fileName));
header("Content-Type: text/csv");
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($filePath));
@
readfile($filePath);
unlink($filePath);
}
如遇到: PHP fopen()错误:无法打开流:权限被拒绝 解决办法如下:也不一定全对,不过我是用这种方法解决的。很简单
$filePath = '/main/temp/csv/' . $fileName;
如这行代码所示,/main/temp/csv/ 这是临时文件保存的位置,在这个位置新建一个空文件夹设置这个文件夹的权限为 755 解决问题,希望能对遇到相同问题的同仁们起到一点作用,这篇文章就不白写了。