This article mainly introduced the PHP download file automatically add BOM Header Method Example, the need for friends can refer to the
First figure out, what is 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). 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 header? Add the BOM header before the output file: 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 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); } */ echo $out;