Page 1 of 1

download inserts utf-8-BOM

Posted: Thu Nov 11, 2010 4:33 pm
by zensys
I use the below code to download a ms word file from a database:

Code: Select all

        header("Content-type: application/msword");
        header('Content-Disposition: attachment; filename='.$fileName);
        
        echo $content;
        die;
This forces a download but the 'download' prepends the utf-8-BOM (the hex signature EF BB BF) which corrupts the file for Open Office en MS Word. Any suggestions as to how this can be avoided? Different PHP configuration perhaps? Million thanks in advance for anyone who can help.

I narrowed down the issue (hours going by) to occurring after the headers are sent. Same happens when the file is read directly from the original file, so it is not a mysql issue. $content has the correct strlen (with mb_strlen) but the downloaded file has increased by 3 bytes. Other headers (transfer encoding, content-length) make no difference nor specifying ansi as charset. I am using LAMP.

This issue turns up a lot at forums but there it is the famous headers already sent problem which for individual files can be resolved easily with a hex editor. I need to avoid the issue occurring in the first place.

Re: download inserts utf-8-BOM [solved]

Posted: Fri Nov 12, 2010 2:56 am
by zensys
Ok, tracked it down. I use the zend framework and the offending BOM was in my bootstrap.php, one of the first scripts in a chain of scripts used to launch the application and finally the download script. So this three hex characters managed to find its way all the way to the output for my download, messing up the result (and a complete working day, hope this post saves someone elses day).

I found the offender with the following command in linux:

grep -rl $'\xEF\xBB\xBF' /path/to/my/project | more