Save or php://output?

Topics: Developer Forum, User Forum
Apr 30, 2010 at 8:14 AM

Now I have another problem.
Heading this way:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=Report.xls");
header("Content-Transfer-Encoding: binary ");
$objPHPExcel = PHPExcel_IOFactory::load($template);

Fill some data with :


And then Im doing:


$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);

I got the message that the file is corrupted and when I open, it is unreadable.

But if I delete the headers and in the end do:

The file is saved properly.
What could be happening?

Thank you in advance

May 1, 2010 at 4:45 PM

It's likely that your script is outputting something to the browser other than the headers and the file output... this is the normal cause of a corrupted file because that output is then embedded in the file.

If you open the downlaoded excel file in a text editor, you might be able to identify any message or even just white space that is being output, although even an echoed space can cause problems. Other characters that may cause issues are BOM markers at the beginning of your script.

May 3, 2010 at 3:56 PM
Thank you for your response, one more question what ar BOM markers ? Thank you in advance.
May 3, 2010 at 5:43 PM

BOM markers a a series of data bytes at the beginning of a file or script that indicate the script can use UTF-8 characters rather than plain ASCII characters

May 3, 2010 at 8:53 PM
Edited May 3, 2010 at 8:54 PM
Ok, i have in my data some special characters like "ñ" or "á". Those characters are a problem for php://output? which BOM markers should I use? Thank you again
May 3, 2010 at 10:22 PM

BOM markers are a problem in PHP scripts (nothing to do with PHPExcel) because they exist before the opening <?php of your script (most editors don't show them, so you probably won't see them). This means they are treated as HTML output, so they are sent to the browser before any headers, echoes or prints in your script. So you never want BOM markers in your PHP scripts

Special characters are not a problem as long as you work with UTF-8. If you're taking input from an HTML form on a page that isn't UTF-8, or from a database where you're using a character set other than UTF-8, then you'll need to convert those strings to UTF-8 (using PHP's iconv() function) before storing the string in a cell.

May 19, 2010 at 7:10 PM

Use utf8_encode()

Example: $objPHPExcel->getActiveSheet()->setTitle(utf8_encode('Relatório'));

Works for me.

May 20, 2010 at 4:24 PM
Ok, thank you very much

From: [email removed]
To: [email removed]
Date: Wed, 19 May 2010 11:10:09 -0700
Subject: Re: Save or php://output? [PHPExcel:211116]

From: willianrn
Use utf8_encode()
Example: $objPHPExcel->getActiveSheet()->setTitle(utf8_encode('Relatório'));
Works for me.

Tu vida no tiene límites, ahora Hotmail tampoco. 25 GB para organizar y compartir todo. Hacé clic aquí
Aug 27, 2010 at 8:40 PM

if true use utf8_encode me solve the problem of Ñ and to, but .. In some areas I have to print strings that include special characters such as $, u$s, ', ".and I could not find a way to excel at me go, you know how?