在csv php中下载文件
我编写了一个函数,用于在php中下载csv格式的文件,如下所示:-在csv php中下载文件,php,csv,download,moodle,Php,Csv,Download,Moodle,我编写了一个函数,用于在php中下载csv格式的文件,如下所示:- function report_download_csv($fields, $data, $filename) { global $CFG; require_once($CFG->dirroot.'/user/profile/lib.php'); $filename = clean_filename($filename.'-'.date('Y-m-d').'.csv'); header("
function report_download_csv($fields, $data, $filename) {
global $CFG;
require_once($CFG->dirroot.'/user/profile/lib.php');
$filename = clean_filename($filename.'-'.date('Y-m-d').'.csv');
header("Content-Type: application/download\n");
header("Content-Disposition: attachment; filename=$filename");
header("Expires: 0");
header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
header("Pragma: public");
$row = array();
$date_field = array('registrationdate', 'expirydate', 'startdate', 'enddate');
$totalrow = count($fields);
$row[] = implode(',',$fields)."\n";
foreach($data as $d){
for($i=0;$i<$totalrow;$i++){
$row_d[] = $d->$fields[$i];
}
$row[] = implode(',',$row_d);
$row_d = array();
}
echo implode($row, "\n");
die;
}
函数报告\u下载\u csv($fields、$data、$filename){
全球$CFG;
需要_once($CFG->dirroot./user/profile/lib.php');
$filename=clean_filename($filename.'-'.date('Y-m-d')。.csv');
标题(“内容类型:应用程序/下载\n”);
标题(“内容处置:附件;文件名=$filename”);
标题(“到期日:0”);
标头(“缓存控制:必须重新验证,后检查=0,前检查=0”);
标题(“Pragma:public”);
$row=array();
$date_字段=数组('registrationdate','expirydate','startdate','enddate');
$totalrow=计数($fields);
$row[]=内爆(“,”,$fields)。“\n”;
foreach($d数据){
对于($i=0;$i$字段[$i];
}
$row[]=内爆(',',$row_d);
$row_d=array();
}
回声内爆($row,“\n”);
死亡
}
但是,当我调用函数时,它只是在浏览器中打印结果。函数中是否有我应该更改的内容?以下标题应强制下载
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Transfer-Encoding: binary");
像这样试试
header('Content-Type: application/csv');
header('Pragma: no-cache');
不。仍然在浏览器中获取所有内容。不。没有强制下载。仍然在浏览器中获取所有内容。