PHP hell with a .csv file

I’ve just spent the morning battling with a piece of code that that pulls about 7000 rows from join over 5 tables, does a bit of post processing and then exports it as a .csv file with about 15 columns.

I hunted down all the bugs until everything worked fine, the script would run and the windows alert box would pop up asking if I wanted to save or open the file, so far so good.

But then try and open the file in Excel and bang:

SYLK: File format is not valid

What the hell does this mean? I spent hours tweaking and adjusting, looking at the file in editplus and OpenOffice – looking for some kind of error to do with un-escaped characters… nothing, not a peep.

As a final last ditch attempt, before I pulled all my hair out, I typed the Excel error message into Google… guess what:

http://support.microsoft.com/kb/323626
“SYLK: File format is not valid” error message when you open file

SYMPTOMS

When you try to open a text file or a comma-separated variable (CSV) file, you may receive the following error message:
SYLK: File format is not valid
Back to the top

CAUSE

This problem occurs when you open a text file or CSV file and the first two characters of the file are the uppercase letters “I” and “D”. For example, the text file may contain the following text:

ID, STATUS
123, open
456, closed

Note This problem does not occur if the first two letters are lowercase “i” and “d”.

How daft is that.

One Response to “PHP hell with a .csv file”

  1. Darfuria

    One of the funniest yet stupidest errors I have ever seen. I’ve been playing an online game recently, and when something errors (which doesn’t occur very often but the game is very new, so it is expect to occasionally) you get a message on your screen which reads “! GENERAL ERROR”, and then when you proceed to move around everything stretches for a few seconds. It’s quite entertaining.