tags:

views:

979

answers:

2

Hey everyone,

I saw a post on this already, but it didn't really provide a solution (that has worked for me at least)...

I have a PHP page that does some basic MySQL queries, the results of which are displayed on the page. I'm using some $_GET and $_SESSION variables throughout this process.

In the same page, I also allow the user to "Export to CSV" (by calling a function). The file returned from the export has the CSV results at the bottom, but also contains the HTML of my page (which calls the function).

Now, at the top of the page I have ob_start() and at the bottom I have ob_flush(), otherwise on page load I will receive some "Cannot modify header..." errors. So, as suggested in the post that I read:

 My guess is that you've got some sort of template that generates the same HTML header and footer regardless of the page that is requested. Sometime before the exportCSV function is called, the header is generated.

 You don't show the bottom of the output, but I'll bet the footer is there too, since I suspect the flow control will continue on to that code after the function exits."

 (http://stackoverflow.com/questions/207019/why-am-i-getting-html-in-my-mysql-export-to-csv/207046)

Does anyone have any ideas on how I can prevent this from happening? Let me know if I should post some of my code and I will...

Thanks!

EDIT:

When calling ob_end_clean() before I call my export function, it gets rid of any html before the CSV. However, I am still getting some HTML closing tags after the CSV results...

 fname  lname MONTH WeekdayCount WeekendCount
 John   Doe 8 1 0
 John   Doe 7 3 2
 Jane   Smith 7 3 2
 </div>    
    </div>   
 </div>    

 </body>       

 </html>

EDIT:

This issue has been solved by calling exit() after calling the CSV export script.

+1  A: 

Without seeing your script it's hard to say what exactly the problem is other than to say that you can't send HTTP headers to the client after you've sent ANY content. This includes white-space. Sometimes you'll have non-printable characters before your opening <?php statement as well. If necessary use a hex editor to find these.

The top of your export.php script should look something like:

<?php
header('Content-Type: text/csv');
...

Is this the case?

cletus
cletus,No this wasn't the case. But I added it and it didn't seem to do anything...
behrk2
+1  A: 

You could try calling ob_end_clean() before you output the csv data, and it should throw away any output you have already printed as HTML.

You could call exit() after outputting your csv data in order to stop the rest of you page being printed.

This doesn't seem a particularly good approach, can you not have a separate PHP script to output the csv which doesn't include the headers and footers by default?

Tom Haigh
Thanks for you suggestion, please see my Edit..
behrk2
Ah nevermind, I forgot to call exit() after outputting the csv. That worked! Thanks alot. Is it bad that I have to be using ob_flush and ob_end_clean() etc. ?
behrk2
The actual script that exports the CSV is separate. But I have to call the script in my existing PHP page (with HTML mixed in)...
behrk2
You could consider splitting your PHP scripts into logic and presentation, i.e. don't output any HTML until you have loaded and processed all the data, and you know exactly what you want on the page.
Tom Haigh