2017-02-01 2 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

은 내가 첨부 파일과 함께 이메일을 보낼 수 있도록 워드 프레스 디렉토리에 저장합니다. 또 다른 질문 - 생성 된 CSV를 디렉토리에 저장하지 않고 전자 메일의 첨부 파일로 보낼 수 있습니까? –

관련 문제