嗨,所有WordPress专家。我想问为什么我会收到错误警告:无法修改标头信息 - 已发送的标头 fputcsv()正在输出但未写入csv且没有强制下载发生。请帮助我。
class mycsv{
function generate_csv(){
global $wpdb;
$sql = "SELECT * from _mamahota_activity WHERE activity_date = '2016-08-07' ";
$MyQuery = $wpdb->get_results($sql);
$csv_fields=array();
$csv_fields[] = 'Activity Date';
$csv_fields[] = 'Name';
$csv_fields[] = 'Activity Name';
$csv_fields[] = 'Email Address';
$csv_fields[] = 'Phone No.';
$output_handle = fopen( 'php://output', 'w' );
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
foreach ($MyQuery as $Result) {
$leadArray = (array) $Result; // Cast the Object to an array
fputcsv( $output_handle, $leadArray );
}
exit;
}
}
答案 0 :(得分:0)
我解决了这个问题。我添加了ob_start()和ob_clean
add ob_start()
class mycsv{
function generate_csv(){
global $wpdb;
$sql = "SELECT * from activity WHERE activity_date = '2016-08-07' ";
$MyQuery = $wpdb->get_results($sql);
ob_clean();
$csv_fields=array();
$csv_fields[] = 'Activity Date';
$csv_fields[] = 'Name';
$csv_fields[] = 'Activity Name';
$csv_fields[] = 'Email Address';
$csv_fields[] = 'Phone No.';
$output_handle = fopen( 'php://output', 'w' );
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
foreach ($MyQuery as $Result) {
$leadArray = (array) $Result; // Cast the Object to an array
fputcsv( $output_handle, $leadArray );
}
exit;
}
}