admin管理员组文章数量:1318958
function generate_pdf($array)
{
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');
$fp = fopen('php://output', 'wb');
foreach ( $array as $line ) {
$val = explode(",", $line);
fputcsv($fp, $val);
}
fclose($fp);
}
$array = array(
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"'
);
generate_pdf($array);
Here's my code generating correct csv file, But When I'm implementing this in my Custom Plugin It's generating Coding in .csv file
function generate_pdf($array)
{
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');
$fp = fopen('php://output', 'wb');
foreach ( $array as $line ) {
$val = explode(",", $line);
fputcsv($fp, $val);
}
fclose($fp);
}
$array = array(
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"'
);
generate_pdf($array);
Here's my code generating correct csv file, But When I'm implementing this in my Custom Plugin It's generating Coding in .csv file
Share Improve this question edited Oct 15, 2020 at 18:41 Tom J Nowell♦ 61k7 gold badges79 silver badges148 bronze badges asked Oct 15, 2020 at 18:05 Mr_UssiMr_Ussi 363 bronze badges 2- 1 Can you rephrase your last sentence? It says your code X but when in your custom plugin it also does X which doesn't make sense, what do you mean by generate coding in CSV file? How are you using the code in your plugin? it isn't clear what the WP version of this looks like or how it misbehaves – Tom J Nowell ♦ Commented Oct 15, 2020 at 18:42
- I think I’ve seen this before... instead of outputting just the CSV or PDF you’re also getting HTML in the output, correct? If so then you’ll need ob_start() at the beginning. Let me see if I can find my solution. – Tony Djukic Commented Oct 15, 2020 at 19:36
1 Answer
Reset to default 0When I attempted something similar I experienced the same issue where the HTML code of the page I was generating the CSV from would be included within the CSV mixed with the data I actually wanted.
The two things I figured out, after searching and reading and reading and searching was to use ob_end_clean();
before I start outputting the content of the CSV. ob_end_clean()
is 'Output Buffers End/Clean` - https://www.php/manual/en/function.ob-end-clean.php
I also added die()
at the end of the CSV output code to, well, exit the process. You can also use exit()
.
function generate_pdf( $array ) {
ob_end_clean(); //ADD THIS LINE
header( 'Content-Type: text/csv' );
header('Content-Disposition: attachment; filename="sample.csv"');
$fp = fopen( 'php://output', 'wb' );
foreach( $array as $line ) {
$val = explode( ',', $line );
fputcsv( $fp, $val );
}
fclose( $fp );
die(); //add this as well
}
本文标签: pluginscode is working properly in Core PHP but writing coding in WordPress
版权声明:本文标题:plugins - code is working properly in Core PHP but writing coding in WordPress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742058496a2418442.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论