setting headers & printing content issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lefnire
Forum Newbie
Posts: 2
Joined: Tue Jun 03, 2008 2:59 pm

setting headers & printing content issue

Post by lefnire »

i have a php page with a button that exports a mysql table to csv. the problem is that the actual html page is printed to that csv file right before the mysql content. in otherwords, my csv output looks something like this:

<html><body>bla bla bla</html></body>
col1, col2
cell1, cell2,
cell3, cell4

and it *should* just be the col/cell stuff, without the html.
my hunch is that i'm messing up with the headers somehow, like I'm failing to clear the current output before re-outputing the csv data or something. My header declarations look like this:

Code: Select all

//$out = mysql conversion to csv
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out;
exit;
and a print_r on $out shows that it's pure mysql output, without any html. the full source is at http://pastebin.com/m11bef0bf
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Re: setting headers & printing content issue

Post by Oren »

Why the heck you are echo-ing $out?? you should be writing it to a file.
lefnire
Forum Newbie
Posts: 2
Joined: Tue Jun 03, 2008 2:59 pm

Re: setting headers & printing content issue

Post by lefnire »

well i tried that, writing to a file then doing readfile(); but that didn't make a difference. should it make a difference?
Post Reply