自动生成文件的第一行为空


First line of an auto genrated file is blank

我正在通过PHP生成一个临时文件,并使用以下代码强制下载:

$liste = "this is a sample text to be displayed in the file"
$filename = "temp_code.txt";
header ("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.basename($filename).'"' );
echo $liste;

这里的问题是第一行是空行,输出从第二行开始。这是这个脚本的输出:

1 . 
2 .  this is a sample text to be displayed in the file

我需要做的是避免生成第一行。

一个更好的方法是使用这样的数组。

$foo = array();
while($a = mysql...)
{
  $foo[] = $a['key'];
}
//your headers
echo implode(PHP_EOL,$foo);

当我在关闭PHP的?>或简单地删除?> 后删除使用enter key生成的新行时,这一行消失了