我有一个数据库,希望用户将数据作为csv文件下载。用户可以选择要下载的数据范围,即从选定的日期到当前日期。我希望csv文件被命名为'selected-date.csv‘
我使用了以下代码:
$filename = $date . "csv";
header ('Content-Type:text/csv');
header ('Content-Dispostion: attachment; filename="'.$filename.'"');该文件是下载的,但文件名(get.php)是此代码所在的。我也尝试过提供一个默认的文件名,比如"download.csv“,但这也不起作用。不管我做什么,文件都是用php文件的名称下载的。这是通过Chrome,Firefox和IE。我没有Safari来试穿。日期为YYYY DD格式。因此,我不认为有任何无效的字符可以引发任何错误。
我在这里做错什么了?
发布于 2016-01-07 06:32:13
您必须设置文件下载的标题。
downloadfile($filename) {
$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");
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
//file name
$filename = date("Y-m-d") . ".csv"; // make sure it is .csv ( i can not see . in your case )
//call function
downloadfile($filename);https://stackoverflow.com/questions/34648455
复制相似问题