如何在WordPress中通过我的插件保存生成的csv文件?

时间:2017-02-01 10:32:28

标签: wordpress csv

我正在创建一个生成用户CSV文件的插件,我想用唯一名称保存生成的csv文件。

我目前的代码: -

$output_filename = 'members-' . date('F j, Y') . '.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, $header_row );
    foreach ( $data_rows as $data_row ) {
      fputcsv( $output_handle, $data_row );
    }
    fclose( $output_handle );
    die();

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您需要从浏览器下载此文件吗?

如果是,请尝试使用此标题:

$output_filename = 'members-' . date('F j, Y') . '.csv';

// Disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");

// Force download  
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

// Encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");

fputcsv( $output_handle, $header_row );
foreach ( $data_rows as $data_row ) {
  fputcsv( $output_handle, $data_row );
}
fclose( $output_handle );
die();