PHP download files automatically add BOM header, first need everyone to find out, what is the BOM header? When you save a text file in UTF-8 format with a program like Notepad under Windows, Notepad adds a few invisible characters (the EF BB BF) to the front of the file, which is the so-called BOM (Byte Mark).
In the Utf-8 encoded file BOM in the file head, occupies three bytes, used to indicate that the file belongs to the Utf-8 code, now has a lot of software to identify the BOM header, but some do not recognize the BOM header, such as PHP can not identify the BOM header, This is also the reason why you can make mistakes when you edit the Utf-8 encoding with Notepad.
Not limited to Notepad saved files, as long as the openings in the file contain the EF BB BF several invisible characters (16 should be xefxbbxbf, visible with binary editing files). This is like a conventional thing, when the system sees this thing, it will feel that you this file is UTF-8 code.
If your interface is UTF-8, you need to force download a file, such as Csv.excel in the default (Chinese background), that CSV is GB code, so if the meter has a BOM head, then you give the user presented files, may be garbled.
How to add BOM head?
You can add a BOM header before you output the file:
The code is as follows:
FileName
$filename = "www.jb51.net. csv";
Header (' Expires: ' Gmdate (' d, D M Y h:i:s ', $_server[' request_time '] + 10). ' GMT ');
Header (' cache-control:max-age=10 ');
Header (' Content-type:application/vnd.ms-excel; Charset=utf-8 ');
Header (' content-type:text/csv; Charset=utf-8 ');
Header ("content-disposition:attachment; filename={$filename} ");
If there is a hint in the result, change the first row output to the prompt message text
$out = "XEFXBBXBF";//Plus BOM header, the system automatically defaults to UTF-8 encoding
if (!empty ($extra [' notice '])) {
$out. = "{$extra [' notice ']}rn];
}
Output
foreach ($table as $row) {
$out. = Implode (",", $row). "RN";
/* if (mb_detect_encoding () ($out) = = ' UTF-8 ') {
$out = iconv ("Utf-8//ignore", "GBK", $out);
The following content to explain the BOM header and remove methods
The way to get rid of the BOM header is simply the following two kinds:
1, EditPlus to the method of BOM head
After the editor adjusts to the UTF8 encoding format, the saved file is preceded by a string of hidden characters (also the BOM), which is used by the editor to identify whether the file is encoded in UTF8.
Run EditPlus, click Tools, select Preferences, select files, UTF-8 logo always delete the signature, and then edit and save the php file after the PHP file is not with the BOM.
2, UltraEdit remove the BOM head method
After opening the file, save as an option in the encoding format selected (Utf-8 no BOM header), ok OK
It's easy to get rid of the BOM header.
Another piece of discussion utf8 BOM information
BOM refers to the storage of the PHP file itself as a UTF-8 with the BOM, ordinary pages of Chinese garbled method is generally not caused by this reason.
Header ("content-type:text/html; Charset=utf-8 ");
This sentence controls the HTML output page encoding, the BOM only in Windows under the "notepad" stored as UTF-8, this can be used to winhex the beginning of the 2 bytes deleted.
Within the Dreamweaver coding settings can be set up with a BOM, generally as long as the output of PHP is not a picture (GDI Stream), the BOM will not cause problems.
GDI Stream appears as a red fork if it has an extra character at the beginning.
The above content for you to introduce the PHP download file automatically add BOM header and explain the BOM header and remove methods, I hope you like.