创建CSV并强制下载

时间:2018-05-29 20:25:31

标签: php csv download

我想写一个CSV并强制自动下载它。它只显示文件。我的代码:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');


// open to write

$fp = fopen($fileName, 'w');  

// MS-Excel BOM:

fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));

// write columns

foreach ($columnNames as $fields) {
  fputcsv($fp, $fields, ';');
}

$exportData  = array (
array($value1, $value2, $value3)
);


foreach ($exportData as $fields) {
  fputcsv($fp, $fields, ';');
}


fclose($fp);

我已经试了几个小时。什么都做不了。它只会被显示。

1 个答案:

答案 0 :(得分:0)

您可以尝试将该文件添加到zip文件中,并要求访问者更改其设置,以便自动下载zip文件。

https://www.tipsandtricks-hq.com/forum/topic/force-a-file-to-download-instead-of-showing-up-in-the-browser