Pregunta

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');
¿Fue útil?

Solución

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;

Otros consejos

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 .

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)); // <-- 
}

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

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>

If someone is still facing above problem

Please change content type as

header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top