0

PHP - I have read and used many of the answers, but I keep getting the HTML
of the page as well, how do I stop this!!, or am I just stupid, no don't
answer that. (the new formatted version, sorry guys!!!)

Here is the code:

function WriteCSVFile( $csvArr, $nLine )  
{  
    header( "Content-type: text/csv" );  
    header( "Content-Disposition: attachment; filename=file.csv" );  
    header( "Pragma: no-cache" );  
    header( "Expires: 0" );  
    header( "Cache-control: private" );  
    header( "Cache-control: must-revalidate, post-check=0, pre-check=0" );  

    for( $nLoop = 0; $nLoop <= $nLine; $nLoop++ )  
        echo $csvArr[$nLoop];  
}
4
  • 1
    Use the MIME type text/csv for CSV files. That should make your browser treat it like a CSV. Commented Apr 11, 2011 at 4:07
  • Also, please indent any code by 4 spaces. This is unreadable. Commented Apr 11, 2011 at 4:08
  • Thanks Amadan, I thought I did, will try hardeer next time. Commented Apr 11, 2011 at 5:07
  • Do you have any output before calling 'WriteCSVFile' function? Commented Dec 21, 2012 at 7:28

3 Answers 3

1

You are looping the following before you ouput any headers:

for( $nLoop = 0; $nLoop <= $nLine; $nLoop++ )
    echo $csvArr[$nLoop] . "<br />";

Stick that after your header()

EDIT

As I did this 'before' you changed it, I will still leave the for loop still. One thing I will point out is that you are calling a function WriteCSVFile( $csvArr, $nLine )

But you never tell us how this fn() is being called, it could be that something BEFORE this function is outputting and therefore you have html, your headers won't matter if you already have something outputting to the user.

Check what is being output (what HTML??) then search for that exact html in your php code.

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

4 Comments

Thanks heaps for that, but if you notice I did that, the first loop is a dummy debug for me, to make sure i have the correct info, just to make sure it wasn't affeting it I commented it out as follows:
Note first loop is removed, but still outputting HTML
@Hans Engel - Thanks Hans, changed as above, but again still getting HTML, I've double checked code to make sure array is clean.
Thanks, I think you are right, the page is a potpourri of html, js, css & php which reads data from a MySQL db and displays to end user, however the end user also needs to dowload result to an excel file. So I am stuck. I think I might create a table on the fly and try exporting that to CSV. What do you think??
0
for( $nLoop = 0; $nLoop <= $nLine; $nLoop++ )
    echo $csvArr[$nLoop] . "\n";

If the items of $csvArr are arrays itself, you probably want to implode() the values to a single line

for( $nLoop = 0; $nLoop <= $nLine; $nLoop++ )
    echo implode(',', $csvArr[$nLoop]) . "\n";

Comments

0

Please try.

header("Content-type: application/csv");

Or check whether you have any output before calling the WriteCSVFile function. If so it will return erros saying can't modify header information.

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.