8

I have strange error and not sure how to tackle it without wasting too much time. I have a method in my controller which should return xml using:

  header("Content-type: text/xml");
  header("Content-Disposition: attachment; filename=output.xml");
  header("Pragma: no-cache");
  header("Expires: 0");

the thing is that the output is not valid xml because of empty line and I have no idea from where it comes, do you have an idea how to fix this? maybe ignore this empty line or something? I do not want to debug the whole framework... I tried to use var_dump(debug_backtrace()) but I get one big mess probably because of doctrine.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
mkk
  • 7,583
  • 7
  • 46
  • 62

7 Answers7

20

Almost always, there will be an empty line before or after your <?php ?> tags. If not in your main file, look at your includes.

Also, a little tip... if your file is pure PHP, just start it with <?php and never close the PHP tag. Closing it isn't necessary, and then you avoid blank lines at the end causing you trouble.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • 3
    a tip to sort the ending one is not to close php tags unless you really have to – Shaun Hare Apr 10 '12 at 20:44
  • @ShaunHare, I think you added that comment just as I added it to my answer? Or, am I confused? What do you mean? – Brad Apr 10 '12 at 20:45
  • No must have clashed ! mean the same – Shaun Hare Apr 10 '12 at 20:46
  • I am facing this issue. In my application, we provide subscribe calendar using webcal URL. Looks like somewhere new line is output before outputting ICS file. I am not able to find any file which is doing this. I use die( $content) to output the ICS file. Is there any way to remove any unwanted newlines just before calling die() ? – Krishna Shetty Jun 11 '13 at 14:08
  • @KrishnaShetty, Open up a new question. You probably have a blank line in one of your PHP files. – Brad Jun 11 '13 at 14:23
  • Thank you man! I was doing some js injection and spaces were causing a sintax error. Not closing php tag solved problem! Thx! – Boy Jun 18 '16 at 06:01
  • 1
    Damn it! I had the same problem like mkk! You give me the solution! Great! Thank you! – Guybrush Apr 10 '18 at 08:09
  • 1
    You're a life saver, just remove and closing php tags from your included files. – Nabil Ali Jul 27 '18 at 11:54
7

Use ob_clean() which clears buffer that had unwanted newline

Masih
  • 1,633
  • 4
  • 19
  • 38
5

Also, any error messages will be printed into the document before the headers. but sometimes it's hard to find the offender when there's no error. Try this:

ob_start()

Put that first thing in your script, this will ensure that nothing else is output until you call

ob_flush()

Which may not be necessary for you (the ob_flush).

Also, I had this issue a while ago where a script that was included in the middle of another script had a single character after the closing php tag, which caused that single character to push the headers down and make the response invalid.

So I would follow the suggestion made by Brad that you don't use closing php tags unless you have to.

Kasapo
  • 5,294
  • 1
  • 19
  • 21
2

I run into this issue from time to time. And it can be very frustrating. :-) Either way - I find this helpful. This will show files with an empty line followed by a php start tag. It will show false positives but helps narrow the search down nicely.

pcregrep -Mron '\n\<\?php' .
Bas Kuis
  • 748
  • 1
  • 7
  • 20
1

I had the same issue and the problem came from a blank line after a closing ?> at the end of one of my php files.

Double check all your php files and remove the closing ?>

0

You almost certainly have an empty line somewhere before or after your PHP tags. One way to fix it is to always exit or die() after outputting the XML file. This will cut off any extra lines you may have at the end of your code. It's up to you to make sure there's none at the start of the file, though.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

After Plesk update I had an empty file /tmp/.tmp.err which was autoprepended with auto_prepend_file in php configuration.

So, all PHP scripts were prepended with an empty string. Some unique situation, but probably could help to somebody.

Fedir RYKHTIK
  • 9,844
  • 6
  • 58
  • 68