移动到 F3 class 时写入 php://输出失败?
Writing to php://output fails when moved to F3 class?
我有以下几行代码:
$objPHPExcel = new PHPExcel();
..ADDITIONAL CODE..
// Redirect output to a client.s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="powers-id_report.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
当从 PHP 文件(在 class 之外)执行此代码时,会创建 Excel 文件并将其保存到浏览器的下载目录中。
然后我将所有代码移到了它自己的 class:
class ReportController extends BaseController {
function generate() {
$objPHPExcel = new PHPExcel();
..ADDITIONAL CODE..
// Redirect output to a client.s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="powers-id_report.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
$this->fw->reroute('/');
}
}
它失败了。我正在使用 Fat Free PHP 将请求重定向到此 class。我使用断点来查看它仍然像以前一样执行所有代码,但没有文件被输出。 $objWriter->save('php://output');
返回 true
这应该意味着它成功保存了文件。我也试过删除重新路由。我的错误日志也都是空的。
解决方案是
$objWriter->save('php://output');
die();
这会阻止执行 $f3
的控制器功能,例如 afterroute
,这可能会改变 headers。
我有以下几行代码:
$objPHPExcel = new PHPExcel();
..ADDITIONAL CODE..
// Redirect output to a client.s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="powers-id_report.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
当从 PHP 文件(在 class 之外)执行此代码时,会创建 Excel 文件并将其保存到浏览器的下载目录中。
然后我将所有代码移到了它自己的 class:
class ReportController extends BaseController {
function generate() {
$objPHPExcel = new PHPExcel();
..ADDITIONAL CODE..
// Redirect output to a client.s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="powers-id_report.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
$this->fw->reroute('/');
}
}
它失败了。我正在使用 Fat Free PHP 将请求重定向到此 class。我使用断点来查看它仍然像以前一样执行所有代码,但没有文件被输出。 $objWriter->save('php://output');
返回 true
这应该意味着它成功保存了文件。我也试过删除重新路由。我的错误日志也都是空的。
解决方案是
$objWriter->save('php://output');
die();
这会阻止执行 $f3
的控制器功能,例如 afterroute
,这可能会改变 headers。