我为客户端的Wordpress网站构建了一个导入脚本插件。然后我构建了一个导出脚本,但.csv文件不会下载。我认为这是因为我在Wordpress框架内工作?
这是我的代码 - 此文件包含在插件中:
if(isset($_POST["exportcards"])){
global $wpdb;
$cards_result = $wpdb->get_results('SELECT wp_posts.post_title as `Card Number`, store_selector.meta_value as `Sponsor`, users.user_email as `Card Owner`, users.user_registered as `Date Registered` FROM wp_posts left join wp_postmeta store_selector on wp_posts.ID = store_selector.post_id and store_selector.meta_key = "store_selector" left join wp_postmeta card_owner on wp_posts.ID = card_owner.post_id and card_owner.meta_key = "card_owner" left join wp_users users on card_owner.meta_value = users.id where wp_posts.post_type = "card" LIMIT 0, 10');
if (! $cards_result) {
$Error = $wpdb->print_error();
die("The following error was found: $Error");
} else {
$csv_fields=array();
$csv_fields[] = 'Card Number';
$csv_fields[] = 'Sponsor';
$csv_fields[] = 'Card Owner';
$csv_fields[] = 'Date Registered';
$output_filename = 'user_cards.csv';
$output_handle = @fopen( 'php://output', 'w' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Description: File Transfer' );
header( 'Content-type: text/csv' );
header( 'Content-Disposition: attachment; filename=' . $output_filename );
header( 'Expires: 0' );
header( 'Pragma: public' );
fputcsv( $output_handle, $csv_fields );
foreach ($cards_result as $Result) {
$leadArray = (array) $Result;
fputcsv( $output_handle, $leadArray );
}
fclose( $output_handle );
echo "Export successful!";
die();
}
}
?>
<form name="exportcards" id="exportcards_form" method="POST" enctype="multipart/form-data" accept-charset="utf-8" >
<input class="button-primary" type="submit" name="exportcards" id="exportcards_btn" value="Download Cards" />
</form>
当我点击&#34;下载卡片按钮&#34;时,它应自动下载.csv文件,但不是。
如果我print_r结果,它执行得很好。我认为,既然我们在Wordpress框架内,标题就无法更新。
有解决方法吗?