The bom header is actually a very hateful thing. Sometimes, due to the bom header, the page may be garbled, but sometimes it is necessary to add the bom header, next, let's take a look at how to automatically add the bom header when downloading a PHP file. I hope the article will be helpful to you. The bom header is actually a very hateful thing. Sometimes, due to the bom header, the page may be garbled, but sometimes it is necessary to add the bom header, next, let's take a look at how to automatically add the bom header when downloading a PHP file. I hope the article will be helpful to you.
Script ec (2); script
First, find out what is the bom header? When you save a text file in UTF-8 format with a program such as notepad in Windows, notepad will add a few invisible characters (ef bb bf) before the file header ), is the so-called BOM (Byte order Mark ).
It is not limited to files stored in notepad, as long as the file's opening contains several invisible characters of ef bb bf (The hexadecimal format should be xEFxBBxBF, which can be seen by binary editing files ). This is like a convention, and when the system sees it, it will feel that your file is UTF-8-encoded.
If your interface is UTF-8, You need to force download a file, such as csv. excel considers csv as GB encoded by default (in Chinese), so if the rice has a bom header, the file you present to the user may be garbled.
How can I add a bom header?
Add the bom header before the output file:
The Code is as follows: |
|
// File name $ Filename = "file.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 prompt in the result, change the first line of output to the prompt text. $ Out = "xEFxBBxBF"; // With the bom header added, 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 ); }*/ Echo $ out; |