14

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');

6 Answers 6

34

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;
2
  • 5
    Thanks god that this solution works, but may I ask why exit; solves the problem?
    – cytsunny
    Sep 19, 2017 at 4:12
  • 1
    Does anyone know why exit() fixes this? Jun 11, 2019 at 12:40
1

You can try this header:

header('Content-type: application/ms-excel');

or check http://www.solutionoferror.com/php/phpexcel-not-downloading-in-mobile-safari-81107.asp .

2
  • you can use javascript window.location.href = "stackoverflow.com/file.xls"; and open that xls source Oct 14, 2013 at 15:38
  • Well it kind of works. Not very elegant, but yes, if I save file first, and then point browser to it - it is saved as .xls. Can you post it as an answer, so I can accept it?
    – Davinel
    Oct 14, 2013 at 16:07
1

I have a similar problem and i solved it with the exit function (used parameter status).

In model:

public static function xls($id)
{
    $xls = new PHPExcel();
    //Code ...
    $objWriter = new \PHPExcel_Writer_Excel5($xls);

    ob_start();
    $objWriter->save('php://output');
    $excelOutput = ob_get_clean();

    return $excelOutput;
}

In controller:

public function xls($id)
{
    header('Expires: Mon, 1 Apr 1974 05:00:00 GMT');
    header('Last-Modified: ' . gmdate('D,d M YH:i:s') . ' GMT');
    header('Cache-Control: no-cache, must-revalidate');
    header('Pragma: no-cache');
    header('Content-type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename=' . $id . '.xls');

    return exit(Controller::xls($id)); // <-- 
}
1
  • Do you know why the exit() fixes this issue? Jun 11, 2019 at 12:40
0

You can use javascript code

<script>
 window.location.href = "stackoverflow.com/file.xls";
</script>

This will open that xls source and file will be available for download

0

For anyone having this problem it is the browser doing its own thing. Using JavaScript worked for me but you need to add html than use JavaScript to output to the browser.

<!doctype html>
<html>
     <head> <meta charset="UTF-8"> <title>Untitled  ocument</title> </head> 
     <body> 
          <script> window.location.href = http://example/file.docx"; </script> 
     </body>
</html>
0

If someone is still facing above problem

Please change content type as

header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.