Shinigami
Shinigami

Reputation: 77

First line of an auto genrated file is blank

I am generating a temporary file via PHP and forcing it to be downloaded using this code:

$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;

The problem here is that the first line is a blank line and the output starts in the second line. this is the output of this script:

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

What I need to do is to avoid generating this first line.

Upvotes: 1

Views: 1326

Answers (2)

Shinigami
Shinigami

Reputation: 77

This line disappeared when I deleted a new line made using enter key after closing the ?> of PHP or by simply deleting the ?>

Upvotes: 1

sanderbee
sanderbee

Reputation: 703

A better way to do this is by using an array like this.

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

Upvotes: 1

Related Questions