Corrupted Output File

Topics: User Forum
Nov 13, 2012 at 12:07 PM

Hey guys, I'm new to PHPExcel and fighting for the last hours to get a valid output file to download. I took 01simple.php and 01simple-download-xlsx.php from the docs, the first one generating a valid XLSX on the file system, the second one making the client download a file which looks ok when opened with a text editor (i.e. starting with the PK mark, no readable error message or anything).

Debugging this problem I merged the two files into one and the last few lines read:

// Redirect output to a client's web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

$objWriter->save('php://output');
$objWriter->save(str_replace('.ctp', '.xls', __FILE__));

exit;

The line with php://output makes the client download a file which Excel complains is corrupted while the line with str_replace works perfectly fine.

I'm looking for ways to get the downloaded file to work. Any suggestions? Could it be encoding? I'm using UTF8 without BOM for all my project files as the website is not displayed correctly otherwise.

 

On another note: Which directories need read and/or write access for PHPExcel to work when only having downloadable files?

 

Thanks,

Daniel

Nov 13, 2012 at 3:23 PM

Tracing the problem some more, I think it's an encoding problem rather than an I/O problem. When I open the downloaded XLSX with TextWrangler it says 'Incorrectly formed UTF-8'. But why would this only appear when downloading the file? I have also tried changing the encoding of the PHP files I'm using to UTF-8 rather than UTF-8 w/o BOM, which didn't change anything.

Any suggestions?

(I didn't change PHPExcel files, so I'm assuming after installation they're still UTF-8.)

Coordinator
Nov 18, 2012 at 4:07 PM

Normally, this error means that you're echoing something from your scrip in addition to the actual Excel file directed to php://output. This can normally be identified by opening the downloaded file in a text editor and looking for anything unusual at the beginning or end of the file. This could be leading or trailing whitespace characters such as spaces, tabs, new lines; or sometimes a BOM heading from a PHP scipt; or may be PHP error messages