當用戶點擊一個按鈕,它會下載一個csv文件。它適用於IE9和Chrome,但不適用於Firefox。在Firefox 20中,內容類型未被設置,因此它將作爲Firefox HTML文檔進行下載。PHP 5.3下載CSV
$filename = 'exportedLogs.csv';
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=" .$filename);
header("Expires: 0");
header("Pragma: public");
$csv_file = fopen('php://output', 'w');
$header_row = array('id', 'project', 'customer', 'time spent');
fputcsv($csv_file, $header_row);
foreach ($logs as $log) {
$log = array(
$log['Log']['id'],
$log['Log']['project_id'],
$log['Log']['customer_id'],
$log['Log']['time_spent']
);
fputcsv($csv_file, $log);
}
fclose($csv_file);
你有沒有試着用'內容Type'(注意是大寫的T)? – Uby 2013-04-11 17:09:41
是的,我也嘗試在Content-type中添加charset = UTF-8。 – Andrew 2013-04-11 17:11:19
也許多一個標題選項會有所幫助。請參閱http://davidwalsh.name/php-force-download – ebadedude 2013-04-11 17:19:16