5

I am trying to generate an XML file in a PHP web application:

<?php
... 
header('Content-Type: application/xml');
header('Content-Disposition: attachment; filename=test.xml');
echo "<?xml version=\"1.0\"?>\r\n" . 
...

Bizarrely, when using my servers (PHP Version 5.3.8/Apache 2.2.17 and PHP Version 5.3.10-1/Apache 2.2.22 respectively) a line feed (hex 0a) character is inserted in the beginning of the output, resulting in invalid XML that cannot be used. There's one more online question about this, unresolved.

So if I try echo "bug"; I get 4 bytes, not 3: 0a 62 75 67

However, when using WAMP server locally (PHP 5.4.3/Apache 2.4.2), I get 3 bytes: 62 75 67.

  • Is this a known bug/feature?
  • Is it a configuration issue?
  • Which is to blame, Apache or PHP?
  • Do I have to upgrade my servers? I'd rather not.
Community
  • 1
  • 1
Gruber
  • 4,478
  • 6
  • 47
  • 74
  • 1
    What do you get if you don't output the headers? Like what if your whole file is ` – Jason Swett Mar 22 '13 at 19:22
  • @Jason Swett: Yes, just tested it. – Gruber Mar 22 '13 at 19:27
  • Gotta ask it, you are certain there is no whitespace before opening ` – ficuscr Mar 22 '13 at 19:31
  • @ficuscr: No whitespace in the beginning. Verified it in a binary editor. – Gruber Mar 22 '13 at 19:34
  • 1
    Test serving `test.html`? See if Apache is culprit? Test with command line PHP to see if purely PHP...? BOM certainly comes to mind but 0a isn't valid, should be three bytes for a UTF-8 BOM right? – ficuscr Mar 22 '13 at 19:36
  • 1
    for this code: `ob_start(); echo "bug"; ob_end_flush(); exit;` `0a` exists? – mkjasinski Mar 22 '13 at 19:45
  • @ficuscr: Tried serving one correct and one "`0a`" file. The correct one worked, so it didn't prepend anything. The "`0a`" file didn't improve... Means Apache is probably not the cause. – Gruber Mar 22 '13 at 19:49
  • @mkjasinski right? My first thought was embedded newline in a header. I've seen this with set-cookie headers before. Might be good a good idea to use something like fiddler to better inspect. Gruber, keep removing as many variables as you can, more you can narrow it down the better. – ficuscr Mar 22 '13 at 19:50
  • @mkjasinski: Got 7 bytes: `ef bb bf 0a 62 75 67` – Gruber Mar 22 '13 at 19:52
  • back to UTF-8 encoding and BOM then -> EF BB BF /  Copy and paste from notepad ++ and back :) ? – ficuscr Mar 22 '13 at 19:57
  • 1
    and this: `` - copy identical, without new lines. – mkjasinski Mar 22 '13 at 20:02
  • @ficuscr: Still got the `0a` in front, but the BOM bytes were gone. – Gruber Mar 22 '13 at 20:04
  • @mkjasinski: Hmm, a `20` (space?) instead of `0a` now when I tried your one-liner. Gotta look into that... – Gruber Mar 22 '13 at 20:07

4 Answers4

3

Maybe it's an encoding problem. If you are using UTF8 with BOM, there is an extra character at the beginning of the files. Check the encoding of your files, and convert it to UTF8 without BOM to avoid this extra character.

Jose Armesto
  • 12,794
  • 8
  • 51
  • 56
  • I suspected BOM could be involved, but AFAIK it is unrelated to the `0a` character. Notepad++ reports the file is of type UTF8 without BOM. Tried UTF-8 as well. Same results for both. – Gruber Mar 22 '13 at 19:41
3

It seems like the 0a problem was caused by a trailing Enter character in a PHP file included by my main PHP file. When I removed it from the include file, the 0a character in my output disappeared.

What I find peculiar about this is the different handling of whitespace between PHP versions that I experienced, and the fact that I still got the 0a when testing the community's suggestions.

I have no more time to put research into this, but my advice to people experiencing similar problems is to check whether whitespace in include files may play into the equation. In addition, avoid ending the <?php tag as suggested by Dan below.

Gruber
  • 4,478
  • 6
  • 47
  • 74
  • 1
    If your PHP file isn't a template file, it is a wise idea to never close the ` – Dan Apr 03 '13 at 08:01
3

I just ran into this. Took me and team an hour to track down. It's amazing. I love PHP so much.

This is what caused it for us:

<php
  $code = "blah";
?php>

<EOF>

NOTE THE NEWLINE AT THE END OF THE FILE

PHP, in all its transcendent beauty, will interpret this newline as something you're actually trying to send to someone down the wire. It is HTML after all. As a result, a maximum of one newline character will be prepended to your response, even if multiple PHP files in your include path end with a newline.

Take special care that any file you include or require, if they end in a newline, anything you echo or print from your current file will be prepended with a newline.

JUST. AWESOME.

zian
  • 483
  • 1
  • 5
  • 9
2

In my case it was the newline at the beginning

enter image description here

Monomachus
  • 1,448
  • 2
  • 13
  • 22