download inserts utf-8-BOM
Posted: Thu Nov 11, 2010 4:33 pm
I use the below code to download a ms word file from a database:
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.
Code: Select all
header("Content-type: application/msword");
header('Content-Disposition: attachment; filename='.$fileName);
echo $content;
die;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.