|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-05-24 15:50 UTC] jeff at pointhere dot net
Description:
------------
When using the header() function to prepend a text file so that a user can save a downloaded file, the resulting file has two extra characters on the front of the file (hex '200A' - space and line-feed). When header() functions are removed, the file appears to load into the browser correctly.
Reproduce code:
---------------
// Start sending the file
ob_start();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: text/plain");
header("Content-Length: " . $filesize);
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
header("Content-Transfer-Encoding: 7bit");
echo($cardprint_file);
ob_end_flush();
exit;
Expected result:
----------------
The user's browser should open a window that asks to Open, Save or Cancel the download. When saving or opening the file the contents of $cardprint_file should be the ONLY contents (the contents were verified to be correct with the var_dump command).
Actual result:
--------------
The first two characters in the file (which translate to a blank line) are an ASCII hex 20 (space) and an ASCII hex 0A (Line feed).
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 05 13:00:02 2025 UTC |
THAT DID IT!! I then started experimenting with the code - I took out all of the code that had any variables and it worked. As soon as I added them in it did not. Then I changed the code to work with apostrophes rather than quotes and put the variables back in with a concatonation and it worked! I hope this makes sense and leads to a fix using the quotes and replacable variables. This code works as expected and you can color me thrilled: // Start sending the file ob_start(); header('Pragma: public'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Content-Type: text/plain'); header('Content-Disposition: attachment; filename="'.$filename.'"'); header('Content-Transfer-Encoding: 7bit'); echo($cardprint_file); ob_end_flush(); Thank you so much for pointing me down the path that led me to a work-around and a working program!!! JeffI have a similar problem. If I use: header("Content-type: application/json"); to a page that will serve actual json code, the response gains 4 spaces (char 32) at the beginning. Ok, this do not break the script, json still works but I am questioning the fidelity of content, as those 4 spaces do not exist in original json code but appears in the output when I set the header. If I comment header line, the spaces disappear immediatelly. Already tried to replace quotes by apostrofes (') but don't work either. I suspect that header function is adding space internally.