php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41491 header() adds extra x'200A' space/LF to start of file
Submitted: 2007-05-24 15:50 UTC Modified: 2007-05-28 20:10 UTC
From: jeff at pointhere dot net Assigned:
Status: Not a bug Package: HTTP related
PHP Version: 5.2.2 OS: Sun OS 5.10 (Solaris 10)
Private report: No CVE-ID: None
 [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).

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-27 17:50 UTC] iliaa@php.net
Can you try leaving header() calls with static parameters (no embeded 
variables) and see if you still get the extra chars?
 [2007-05-27 22:18 UTC] jeff at pointhere dot net
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!!!

   Jeff
 [2007-05-28 20:10 UTC] tony2001@php.net
.
 [2013-03-06 20:11 UTC] dosergio at ig dot com dot br
I 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.
 [2015-10-03 19:16 UTC] gorlux at yahoo dot com
It may be worth pointing out that other errors can cause the 200A insertion into downloaded files. In my case an included file (require_once dblogin.php) in my download php script had some space below the closing ?> that was being inserted into the downloaded file, rendering many files unreadable. Plain text and pdf were ok, .doc and .jpg were unreadable. First simple check for download script is to just make sure there's nothing being output outside the <?php and ?> tags in the main file or in any included files.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 12:01:27 2024 UTC