Suddenly problems with downloaded XLS files (corrupt)

Topics: Developer Forum, User Forum
May 19, 2013 at 10:01 PM
I have a PHP script that creates a XLS file for download and worked fine (IIS, PHP 5.4, Win7). Without any code change, suddenly Excel 2010 complains that the file is corrupt.

I first suspected some change in the underlying data caused the problem. But then I tried 01simple-download-xls.php (test script) with the same result. I found another older post about problems with sending the xls to the browser and yes: If I save the file locally, everything is ok.

I also tried 01simple-download-xlsx.php (Excel 2007 format): same problem.

I can only guess that either a PHP upgrade or some Excel patch (making it more picky) cause the problem. LibreOffice can open the file just fine. But if I save it as ODS and then open that in Excel, Excel complains about invalid data (but is able to recover the document).

You can find all files (XLS downloaded to browser, locally saved XLS, and also the slightly modified test scripts (for making them create identical XLS files)) here:

I would really appreciate some help in this, hoping that you can make this excellent project even better!

BR, Oliver
May 19, 2013 at 10:23 PM
Both files open cleanly in Excel2013.

Have there been any changes to your PHP version? Though 1.7.8 has been tested with all versions of PHP from 5.2.0 to 5.4.15 without issues, and I use a variety of versions of Excel to test.
Are you opening the file sent to the browser directly in Excel, or are you choosing to open and then save? Are you using SSL, and IE as your browser?
May 19, 2013 at 10:41 PM
PHP version: there were security updates performed. Don't think this is the problem.

Opening files: You are right. If I open the file from the ZIP archive I published, it works fine in my Excel 2010. This brought me to the root cause: If I go to the downloaded XLS file properties and (under Security) Unblock the file, it opens fine in Excel. (This information is removed when you add a file to a ZIP archive.)

Suspected background: I use SSL (HTTPS). The certificate expired recently and so far I haven't replaced it. In Firefox, I simply created a security exception and Firefox was happy. I guess Excel checks the "origin" of "downloaded" files and refuses to open them if the certificate is expired (or invalid in general)...

Many thanks for your swift response and thanks again for this great tool!

BR, Oliver
May 19, 2013 at 11:47 PM
Well.... small addition: I have replaced the SSL certificate with a StartSSL Class 1 certificate. Firefox, Chrome and IE are happy now.

But the problem with Excel persists, so the certificate is not the problem. So maybe MS increased some security level in Office for documents from untrusted sources (i.e. the Protected view)... There are several post on the Internet, e.g.
May 21, 2013 at 4:50 PM
This suddenly became a problem. Last week all worked well, then now, something has happend and all files are corruped.
I am able to open and repair from Excel, but Without the repair, is not possible to open the file.

Any ideas?
May 21, 2013 at 7:07 PM
Edited May 21, 2013 at 7:07 PM
See my comment from Sun 23:41:
If I go to the downloaded XLS file properties and (under Security) Unblock the file, it opens fine in Excel.
(This means you remove the information that the file was downloaded from the Internet – then Excel is "less picky", it seams.)
Still I don't know why this suddenly appeared.
May 21, 2013 at 10:19 PM
I tried Your solution. But it didn't change the outcome.
I tried to rename it to a .zip and open it as a compressed file. That didn't work either. I got a error on that .zip file as well.
Very strange behaviour.
May 21, 2013 at 10:29 PM
@AGDev - Standard first step when you get this message when trying to open a file in MS Excel - open the file in a text editor and look for leading/trailing whitespace, BOM headers, or any obvious plaintext PHP errors, warnings, notices or messages
May 21, 2013 at 11:38 PM
I have also done that, there is no plain text in the file, except some "known" Excel xml file names like: [Content_Types].xml.
When I open open and repair Excel manages to fix it. After the repair the file grew from 8kb to 10kb. It's like the file is stopped in the compress stage.
I dont save to disk, I pass the file directly to the browser With a Attached file.
May 22, 2013 at 9:26 AM
Edited May 22, 2013 at 9:28 AM
In that case, I haven't got a clue what's wrong: if it won't open correctly when renamed as a zip, then it suggests that there is some corruption in the file. Rather than download to the browser, save to the server's disk then try to open... that will normally identify if it's a problem with spurious characters being injected
May 22, 2013 at 10:59 AM
I have done some more debugging, what I have found is this:

My data is fetched by a multilevel inherited class. (a extends abstractclass, b extends a, c extends b and finally d extends c).
When I use the class normally, without any Excel generation, it Works as excepted. Data is printed to screen and no erros or notices are thrown.
But as soon as I do a $a = New d() in the function that generates the Excel file, I get a corrupt Excel file.

Does this information give any Clues?