2017-02-01 9 views
0

私はユーザーのCSVファイルを生成するプラグインを作成しています。生成されたCSVファイルを一意の名前で保存します。WordPressのプラグインで生成された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(); 

答えて

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(); 
+0

は私が添付された電子メールを送信できるように、WordPressのディレクトリに保存します。別の質問 - 生成されたCSVを添付ファイルとしてディレクトリに保存せずに電子メールで送信することは可能ですか? –

関連する問題