PHP,更改下载文件名

时间:2015-02-24 00:11:13

标签: php csv http-headers webclient-download

我有一个PHP代码,用户可以在其中下载CSV文件:

header( 'Content-Type: text/csv' );
$file = fopen( 'php://output', 'w' );
//-- Write stuff
fclose($file);

通常,浏览器开始将内容下载到文件中。但它始终使用名称" csv.csv"保存下载的文件。有没有办法更改此默认名称?

例如,在上一页中,我可以询问用户他/她想要的名称。

1 个答案:

答案 0 :(得分:3)

您需要设置Content-Disposition标头:

header('Content-Disposition: attachment; filename="file.csv"');

header('Content-Disposition: filename="file.csv"');

header('Content-Disposition: filename="' . $filename . '"');