Corrupt xlsx files when pushing to browser, not saving locally

Topics: Developer Forum, User Forum
Apr 24, 2012 at 6:54 PM
Edited Apr 24, 2012 at 8:04 PM

I am getting corrupted xlsx files when saving/pushing to browser.  If I save the file locally on the disk and then ftp it to my PC, it works fine.  Also have tested on a different server w/ same specs and works fine.  I am totally stuck - have searched forum and saw some posts on php zip being an issue but works on 1 server and not the other with same php and zip versions so not sure that is a problem.


Server with corrupt files produced:

RHEL 5.6

PHP 5.2.12

ZIP 1.8.11

PHPExcel 1.7.6 AND tried SVN from a few days ago


Server working:

CentOS 5.4

PHP 5.2.12

ZIP 1.8.11

PHPExcel 1.7.6 AND tried SVN from a few days ago


Tried opening files in LibreOffice 3.5.2 and MS Office 2003 (don't think that matters either, since CSV file is totally corrupt)

Here is my code:


if ($output_type == "CSV")
  $objWriter = new PHPExcel_Writer_CSV($xlsx);

  header('Content-type: text/csv');
  header('Content-Disposition: attachment;filename="' . $filename . '.csv"');
} //if ($output_type == "CSV")
  // Save Excel 2007 file
  $objWriter = PHPExcel_IOFactory::createWriter($xlsx, 'Excel2007');

  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  header("Cache-Control: no-store, no-cache, must-revalidate");
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Pragma: no-cache");
  header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
} //else




CSV file works fine!


Please help.  Really stuck here!!

Apr 24, 2012 at 8:39 PM
Edited Apr 24, 2012 at 8:39 PM

Just uploaded two xlsx files.  One that is corrupt (report_corrupt.xlsx) which was pushed to the browser.  The other (report_ok.xlsx) was saved locally then ftp'd back and opened fine.

Apr 24, 2012 at 10:41 PM

If you're getting a difference between pushing to the browser and saving to disk, then it's almost certain that the script is outputting additional characters to the browser: this is typically white space characters (space, tab, new lines, etc), or anything (new lines are often a problem here) following a ?>. Check your script for anything that might add these characters.

Apr 25, 2012 at 1:28 AM


Thanks for the reply.  I have read a bunch of posts to that effect and looked for those things and found nothing that would output additional characters.  Since the code works fine on a second server, I don't think its outputting anything additional.


May 7, 2012 at 6:32 PM

I was also experiencing the same problem as you. For some reason I kept getting the corrupted file message when opening up the excel files I sent to the browser. It did turn out to be that the file was sending out additional characters. Once I took them out, the messages stopped and everything worked correctly.