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];
}
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.
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";
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.
精彩评论