Формирую файл:
public function actionExport(){
$list = Tours::find()->select('id,name,code')->all();
foreach ($list as $value) {
$s = $value->name . '|' . Url::base(true) . '/tours/' . $value->code . '/';
$data[] = $s;
}
header("Content-type: text/csv");
header("Content-Transfer-Encoding: UTF-8");
header("Content-Disposition: attachment; filename=export-tours.csv");
header("Pragma: no-cache");
header("Expires: 0");
$outputBuffer = fopen("php://output", 'w');
fprintf($outputBuffer, chr(0xEF).chr(0xBB).chr(0xBF)); //1557 строка
fputcsv($outputBuffer, ['name','url'], ';');
foreach ((array)$data as $val) {
fputcsv($outputBuffer, (array)explode('|', $val), ';');
}
fclose($outputBuffer);
}
Файл скачивается, но в конце файла текст
<pre>An Error occurred while handling another error:
yii\web\HeadersAlreadySentException: Headers already sent in /usr/share/nginx/html/project.eng/modules/admin/controllers/ToursController.php on line 1557. in /usr/share/nginx/html/project.eng/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /usr/share/nginx/html/project.eng/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /usr/share/nginx/html/project.eng/vendor/yiisoft/yii2/web/ErrorHandler.php(135): yii\web\Response->send()
#2 /usr/share/nginx/html/project.eng/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\HeadersAlreadySentException))
#4 {main}
Previous exception:
yii\web\HeadersAlreadySentException: Headers already sent in /usr/share/nginx/html/project.eng/modules/admin/controllers/ToursController.php on line 1557. in /usr/share/nginx/html/project.eng/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /usr/share/nginx/html/russiadiscovery.eng/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /usr/share/nginx/html/russiadiscovery.eng/vendor/yiisoft/yii2/base/Application.php(392): yii\web\Response->send()
#2 /usr/share/nginx/html/russiadiscovery.eng/web/index.php(12): yii\base\Application->run()
#3 {main}</pre>