PHP: How to automatically add a bom header when downloading files-PHP source code

Source: Internet
Author: User
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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.