如何更改直接fopen输出的文件名


How to change the filename of direct fopen output?

我正在使用此脚本显示csv文件:

if (isset($_GET['csv'])) {
    header('Content-Type: text/csv; charset=utf-8');
    $out = fopen('php://output', 'w');
    foreach ($dataArray as $k => $v) {
        fputcsv($out, $v);
    }
    fclose($out);
    exit();
}

和这个Javascript打开csv直接下载:

<a href="#" class="export" onclick="window.open(window.location.pathname + window.location.search + ''&csv=1'');">

现在我想将文件名更改为类似于:

subscriptions_2014_10_23.csv

有人知道怎么做吗?

您需要添加另一个指定文件名的头。然后添加一些日期函数:

$filename = 'subscriptions_' . date('Y_m_d') . '.csv';
header('Content-Disposition: attachment; filename="' . $filename . '"');

只需再添加一个header

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

也许您应该使用头作为文件名:

header('Content-Disposition: attachment; subscriptions_2014_10_23.csv"');