tags:

views:

98

answers:

5

I am using PHP to pass some information in a text file back to the user which is then used as input for an app. I'm using the method shown in the following snippet to serve the file to the user.

header('Content-type: text/csv');
header('Content-Disposition: attachment; filename=filename.dat');
echo $data;
exit();

I get the save as dialogue fine and the data is present the only problem is that there is a line feed character at the start of the output file which I cant seem to get rid of.

It just so happens that the app that uses the data is sensitive to white space and although it would be easy to fix this, forcing the users to update is not a route I want to go down for various reasons.

Does anyone know what is causing this and more importantly any workarounds.

+1  A: 

Probably $data contains the line feed. Look for includes too

The Disintegrator
+1  A: 

Presumably the extra newline is getting into $data somehow. If you can’t fix that, you could trim() the data before you echo it.

Can you post how you're setting $data?

Carey
A: 

Maybe you can use ob_get_contents or ob_get_length to see if anything has been sent to the output before the echo statement. Or use ob_clean before the echo.

Arjan
A: 

I found the solution.

The problem was not in $data but instead as Gumbo suggested the problem was in fact a newline before the ?php tag which was being included in the generated file.

Thanks for the fast answers.

Willbill
+3  A: 

As I already said in the comments to the question:

Either you $data contains that line feed or there is something before that snippet that does this. Maybe a line feed before you opened the PHP block.

Gumbo
Thanks - I assumed incorrectly that only statements after the header call could actually be in the files content
Willbill