Php Yii CSV导出到浏览器使用日志消息而不是数据
我正在尝试使用Yii将CSV导出到浏览器,并且该CSV一直由Yii日志消息填充Php Yii CSV导出到浏览器使用日志消息而不是数据,php,csv,yii,Php,Csv,Yii,我正在尝试使用Yii将CSV导出到浏览器,并且该CSV一直由Yii日志消息填充 header('Content-Description: File Transfer'); header('Content-Type: application/csv'); header('Content-disposition: attachment; filename=enrollment_course.csv'); $enrollment = Reports::getEnrollment($courseId)
header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header('Content-disposition: attachment; filename=enrollment_course.csv');
$enrollment = Reports::getEnrollment($courseId);
$separator=",";
foreach ($enrollment as $users)
{
$tmp = array(
$users->userid,
$users->firstname,
$users->lastname,
);
echo join($separator, $tmp)."\n";
}
Yii::app()->end();
你知道为什么这不能正常工作吗?我建议你在输出完所有内容后就删除脚本。这可以防止向输出中添加任何内容。一个简单的“die”就可以做到这一点。您可以在控制器中使用此代码来禁用所有操作的浏览器日志记录:
protected function beforeAction($action)
{
foreach (Yii::app()->log->routes as $route)
{
if ($route instanceof CWebLogRoute)
{
$route->enabled = false;
}
}
return true;
}
是的,我实际上写了一个扩展来实现这一点: 基本上,它相当于调用exit,请看一下示例的最后几行:
// options for file, changing delimeter and enclosure
$content = $csv->toCSV('../myfilename.csv', "\t", "'");
Yii::app()->getRequest()->sendFile($filename, $content, "text/csv", false);
// call exit instead of letting sendFile do it, because if you are using
// CWebLogRoute, the onEndRequest event will fire and output your log and not csv file :(
exit();