Example of a method for automatically adding BOM headers when PHP downloads a file _php tutorial

Source: Internet
Author: User
First figure out, what is the BOM header? When you save a text file in UTF-8 format with a program such as Notepad under Windows, Notepad adds several invisible characters (the EF BB BF) to the file header, which is the so-called BOM (Byte order Mark).
Not limited to Notepad saved files, as long as the opening of the file contains several invisible characters of the EF BB BF (16 binary should be XEFXBBXBF, with binary edit file visible). This is like a conventional thing, when the system sees this thing, it will feel you this file is UTF-8 encoded.

If your interface is UTF-8, you need to force download a file, such as Csv.excel in the default (Chinese background), that the CSV is GB encoding, so if the meter has a BOM header, then you give the user to present files, may be garbled.

How to add BOM head?
Add the BOM header to the output file before you can:
Copy the Code code as follows:
Filename
$filename = "Www.jb51.net.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 line output to the message text
$out = "XEFXBBXBF";//Plus BOM header, 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;

http://www.bkjia.com/PHPjc/684745.html www.bkjia.com true http://www.bkjia.com/PHPjc/684745.html techarticle first figure out, what is the BOM header? When you save a text file in UTF-8 format under Windows with a program such as Notepad, Notepad adds several invisible characters (... ) to the file header.

  • 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.