problem with downloading excel from php://output

Topics: Developer Forum, User Forum
Oct 12, 2012 at 2:56 PM
Edited Oct 12, 2012 at 3:52 PM

Hi gyus. I am writing with writer an excel5 sheet which queries a database. I cant get the file though even if the file appears to be written to excel from php.


 $excel = new PHPExcel();
   $worksheet = $excel->getActiveSheet();
                     for($col=0; $col<$a2; $col++){
                    for($row=1; $row<count($r);$row++){
$worksheet->setCellValueByColumnAndRow($col,$row,$r[$row][$col]); }} header(\"Content-Type: application/\"); header(\"Content-Disposition: attachment; filename=$legit\"); $objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5'); ob_end_clean(); $objWriter->save('php://output'); $excel->disconnectWorksheets(); unset($excel); } ob_end_flush();?>


from the client i send a request with headers 

'Accept' :'application/',//'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                'Accept-Encoding':'gzip, deflate'


the response seems fine with content length written and all my response headers correctly returned but i dont see a prompt for downloading the file. I trimmed the whitespace from the start of the file.Mind the backslashes before the quotes in the code;its a file written dynamically from php as a string its not faulty.

Yeah and php version is 5.3.8 and PHPExcel 1.7.4 or 1.7.7 anyway the one without the charts support. That code used to work i dont know what happened now.

Oct 13, 2012 at 5:35 PM

Headers should not be:

        header(\"Content-Type: application/\");
        header(\"Content-Disposition: attachment; filename=$legit\");

        header("Content-Type: application/");
        header("Content-Disposition: attachment; filename=$legit");

Look at 01simple-download-xls.php in /Tests for an example

Oct 13, 2012 at 7:57 PM

Thanks for replying. This code is part of a fwrite calling parameter hence the backslashes (It is a string, i forgot to leave the slashes off when pasting the code here). Adding an exit call as the tests file shows doesnt work for me .Do we have to explicity set metadata like creator, etc? The request and response are working fine and i can see them in firebug but i wont get a prompt for downloading. The response is binary. I can see in my mysql log that the queries to the db have been executed correctly. I am working on linux and tried with oasis format to set the headers in client and server in case phpexcel wont render a correct file because no ms excel is available but that doesnt work either. Any other advice Mark?

Oct 13, 2012 at 9:11 PM

This isn't an issue with PHPExcel: but something with the headers... Metadata in the generated file is completely optional; and PHPExcel has no external dependencies - it's deliberately designed to run without MS Excel so that it can be used on any platform

Test by writing the output to a file on disk rather than php://output, and open it normally to verify the state of the file that is being generated.

Oct 14, 2012 at 10:22 AM
Edited Oct 14, 2012 at 11:02 AM

Yes you are totally correct. It is written in the file. And congrats for such a well written and documented project. I thought it was the filename the header problem  but writing 'simple.xls' doesn't solve it. I still cant get a prompt for downloading. It appears this is my last bug, i have a deadline till Tuesday night.

I think i am gonna just send the saved file to the client, thanks.

Oct 14, 2012 at 3:43 PM
Edited Oct 14, 2012 at 3:45 PM

I saw your answer in

and i am using ssl, does this have anything to do with my problem? I can't get a prompt for downloading with readfile()  either. What a mess! Oh and Mark sorry for disturbing you.


    if (file_exists($path)) {
       header('Content-Description: File Transfer');
           header('Content-Transfer-Encoding: binary');
 header('Content-Disposition: attachment;filename=$legit');
  header('Content-Type: application/;charset=UTF-8;');
 header('Content-Length: ' . filesize($path));
  header('Pragma: no-cache');
  header('Expires: 0');

I can upload a screenshot of firebug's console if you like to see that everything is done right.