为什么 CSV 的顶部有一个空行?
why a blank line at the top of the CSV?
我在使用 PHP 构建并强制浏览器下载的 CSV 报告时遇到问题。问题是 PHP 在输出的最开头插入换行符“\n”(0x0A),所以当我打开文件时,我在内容顶部看到一个不需要的 :) 空行.有趣的是,这不会发生在我的本地环境中,只会发生在开发服务器上。
这是我部署到开发服务器以测试 PHP 脚本的示例:
<?php
$doc = array(
array(
"header1",
"header2",
"header3"
),
array(
"value11",
"value12",
"value13"
),
array(
"value21",
"value22",
"value23"
),
array(
"value31",
"value32",
"value43"
)
);
ob_start();
$df = fopen( "php://output", 'w' );
foreach ( $doc as $docLine )
{
fputcsv( $df, $docLine );
}
fclose( $df );
$output = ob_get_clean();
$now = gmdate( "D, d M Y H:i:s" );
header( "Expires: Tue, 012 Jan 1970 00:00:00 GMT" );
header( "Cache-Control: max-age=0, no-cache, must-revalidate" );
header( "Last-Modified: {$now} GMT" );
// force download
header( "Content-Type: text/csv; charset=utf-8" );
header( "Content-Disposition: attachment; filename=\"report.csv\"");
header( "Content-Transfer-Encoding: binary" );
echo $output;
开发服务器是 Ubuntu 14.04.3 LTS
Apache/2.4.7 (Ubuntu)
和 PHP 5.5.9-1ubuntu4.14 (cli)
谢谢。
感谢 Matei,Gracias a Álvaro。 :)
这个问题实际上是 PHP 网站的一个包含文件之后的尾随 \n
。当 php.ini 没有添加任何包含文件夹的规则时,不确定为什么这会影响没有包含的 PHP 脚本。
在此处关注评论 Apache or PHP generating prepending line feed character 我通过创建纯 html 简单页面 .html 扩展名 ;).
检查了 apache 与此问题无关
总而言之,当 PHP 代码是 page/script 中的最后一个时,我建议不要关闭 <?php
标签。
我在使用 PHP 构建并强制浏览器下载的 CSV 报告时遇到问题。问题是 PHP 在输出的最开头插入换行符“\n”(0x0A),所以当我打开文件时,我在内容顶部看到一个不需要的 :) 空行.有趣的是,这不会发生在我的本地环境中,只会发生在开发服务器上。
这是我部署到开发服务器以测试 PHP 脚本的示例:
<?php
$doc = array(
array(
"header1",
"header2",
"header3"
),
array(
"value11",
"value12",
"value13"
),
array(
"value21",
"value22",
"value23"
),
array(
"value31",
"value32",
"value43"
)
);
ob_start();
$df = fopen( "php://output", 'w' );
foreach ( $doc as $docLine )
{
fputcsv( $df, $docLine );
}
fclose( $df );
$output = ob_get_clean();
$now = gmdate( "D, d M Y H:i:s" );
header( "Expires: Tue, 012 Jan 1970 00:00:00 GMT" );
header( "Cache-Control: max-age=0, no-cache, must-revalidate" );
header( "Last-Modified: {$now} GMT" );
// force download
header( "Content-Type: text/csv; charset=utf-8" );
header( "Content-Disposition: attachment; filename=\"report.csv\"");
header( "Content-Transfer-Encoding: binary" );
echo $output;
开发服务器是 Ubuntu 14.04.3 LTS Apache/2.4.7 (Ubuntu) 和 PHP 5.5.9-1ubuntu4.14 (cli)
谢谢。
感谢 Matei,Gracias a Álvaro。 :)
这个问题实际上是 PHP 网站的一个包含文件之后的尾随 \n
。当 php.ini 没有添加任何包含文件夹的规则时,不确定为什么这会影响没有包含的 PHP 脚本。
在此处关注评论 Apache or PHP generating prepending line feed character 我通过创建纯 html 简单页面 .html 扩展名 ;).
检查了 apache 与此问题无关总而言之,当 PHP 代码是 page/script 中的最后一个时,我建议不要关闭 <?php
标签。