4

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.

3 Answers 3

8

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?

Sign up to request clarification or add additional context in comments.

5 Comments

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. ?
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)...
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.
My issue has been solved by calling exit() after calling the CSV export script.
exit works for me. This is in my component using joomla Thanks
2

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?

1 Comment

cletus, No this wasn't the case. But I added it and it didn't seem to do anything...
0

I face this problem also and solved it already. My code is:

<html>
<title> </title>
<body>
       <?php
         ....Code which output MySQL to CSV
       ?>
</body></html>

Below are example of CSV file which come out with HTML code at top.

<html>                      
<head>                  
<meta http-equiv="Content-Language" content="th">                   
<meta http-equiv="content-Type" content="text/html; charset=window-874">                    
<meta http-equiv="content-Type" content="text/html; charset=tis-620">                   

<title> Super Man  </title>                 

</head>                     

<body bgcolor="#FFFFD4">                    

<SQL rows resulted>
xx5497  1/7/2015 1:03   SSFTM   SSFTVM  35  Condition2
xx5498  1/7/2015 1:04   SSDHN   SSDHKN  13  Condition2
xx5499  1/7/2015 1:06   SSFTM   SSFTVM  14  Condition2

When running, CSV file got the first 12 lines on the top. It looked so crazy to me. I solved it by moving the PHP code to the top. Then the result is OK. Only SQL results were output to CSV file.

<?php
  ....Code which output MySQL to CSV
?>

<html>
<title> </title>
<body>

</body></html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.