PHP: Why is this forced mime download adding 2 extra empty lines? -
i have php script wrote create .txt tab delimited file. need have forced downloaded web browser. this, when compare file source forced downloaded on contain 2 blank lines. here code:
// force download of tab del .txt file web browser: header('content-type: application/download'); header("content-disposition: attachment; filename=$tab_del_file"); header("content-length: " . filesize($tab_del_file)); $fp = fopen($tab_del_file, "r"); fpassthru($fp); fclose($fp);
linux shell command compare 2 files , show there blank lines: $ diff example.txt /tmp/example.txt 25a26,27
i sftp'ed downloaded example.txt /tmp directory diff on server. why 2 blank lines being added downloaded version , fix? thanks!
as php code looks ok , not produce new lines, can have 1 reason. have closing ?>
tag , new lines @ end of file:
?> <--- empty line <--- empty line
note content outside php tags not parsed php , forwarded browser.
solution: remove closing ?>
tag or new lines. prefer not using ?>
btw, should mention this:
$fp = fopen($tab_del_file, "r"); fpassthru($fp); fclose($fp);
can simplified by
readfile($tab_del_file);
Comments
Post a Comment