Firefox将文件扩展名更改为“ .csv.htm”。从myapp下载CSV时(php / phpexcel)

时间:2019-07-12 09:12:26

标签: php csv firefox phpexcel

我的应用程序(php / phpExcel)为.csv文件提供了下载功能。当我使用chrome或IE下载文件时,一切正常,但是当我使用Firefox进行下载时,它将文件扩展名更改为“ .csv.htm”,然后Excel询问如何打开,因为扩展名和文件格式无法配合使用。

header('Content-Encoding: UTF-8');
    header('Content-type: text/csv; charset=UTF-8');
    header('Content-Disposition: attachment;  filename="csvdownload_'.date("Y-m-d_H_i_s").'.csv');
    echo "\xEF\xBB\xBF"; // UTF-8 BOM

    // If you're serving to IE 9, then the following may be needed
    header('Cache-Control: max-age=1');

    // If you're serving to IE over SSL, then the following may be needed

    header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); 
    header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header ('Pragma: public'); // HTTP/1.0



    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
    $objWriter->setDelimiter(';');
    $objWriter->setEnclosure('"');
    $objWriter->setLineEnding("\r\n");

    $objWriter->save('php://output');

1 个答案:

答案 0 :(得分:0)

使用下面显示的标题,所有浏览器都应该高兴并以CSV格式下载文件。如果尚未计算字节数,则只需将该标头留在外面即可。

header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-disposition: attachment; filename="Your_File_Name_Here.csv"');
header('Content-length: ' . $FileBytes);
header('Content-type: application/octetstream');
header('Pragma: no-cache');
header('Expires: 0');