download inserts utf-8-BOM

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zensys
Forum Newbie
Posts: 9
Joined: Mon May 31, 2010 5:16 am

download inserts utf-8-BOM

Post 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.
zensys
Forum Newbie
Posts: 9
Joined: Mon May 31, 2010 5:16 am

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

Post 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
Post Reply