Martin Shinks
Martin Shinks

Reputation: 115

Creating CSV file from PHP, no line feed

I am trying to create a CSV file from my web page and am having issue with line feeds/carraige returns.

I am building up the CSV string via Javascript and then adding '\u000A' + "\u000D" to the end of each line.

I then pass my CSV sting to a PHP program via AJAX. My PHP logic to create the file is: -

header('Content-Disposition: attachment; filename="effortTool.csv"');
header('Content-Type: text/plain');
header('Content-Length: ' . strlen($content));

When I view the file it does not contain any line feeds/carraige retuns.

If I output the CSV string as an "alert", I can see it is formatting corerctly. This tells me that the line feeds characters are being added but are being lost when downloaded as a file.

Any ideas?

Thanks

Martin

Upvotes: 0

Views: 110

Answers (2)

Akshay Paghdar
Akshay Paghdar

Reputation: 3629

Use following code to generate CSV file:-

ob_clean();
header("Content-type: application/vnd.ms-excel");
header( "Content-disposition: filename=effortTool.csv");
header('Content-Length: ' . strlen($content));
print $content;
exit;

This was working for me, hope it'll work for you too.

Upvotes: 3

vandango
vandango

Reputation: 567

You put the file content on the meta data of type Content-Length, this is wrong.

Try this: php download file: header()

Upvotes: 1

Related Questions