Safari adding .html to download

Davinel picture Davinel · Oct 14, 2013 · Viewed 10.6k times · Source

I have a little function, that creates .xls document(using PHPexcel) and then sends it to php://output. Then user download it.
Everything works fine, except that safari on mac os x adds .html extension for some reason.
So the resulted file is named report.xls.html. Content is ok, but it is annoying to the users.

How can I resolve this?

Here is part of my code:

$filename = 'report.xls';

header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$filename.'"'); 
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save('php://output');

Answer

BenJsno picture BenJsno · Mar 17, 2014

I had the same problem

Resolved with exit; at the end of the script

$filename = 'report.xls';
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$filename.'"'); 
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save('php://output');
exit;