Home > Backend Development > PHP Tutorial > How to automatically add BOM header when downloading files with PHP and how to explain and remove BOM header

How to automatically add BOM header when downloading files with PHP and how to explain and remove BOM header

WBOY
Release: 2016-07-29 09:07:44
Original
992 people have browsed it

PHP automatically adds a BOM header when downloading a file. First of all, you need to figure out what a BOM header is? When you use a program such as Notepad to save a text file in UTF-8 format under Windows, Notepad will add a few invisible characters (EF BB BF) in front of the file header, which is the so-called BOM (Byte order Mark). ).

In a utf-8 encoded file, the BOM is in the header of the file, occupying three bytes, and is used to indicate that the file belongs to utf-8 encoding. There are many software that recognize the BOM header, but there are still some that cannot recognize the BOM header, such as PHP cannot recognize the BOM header, which is why an error occurs after editing UTF-8 encoding with Notepad.

Not limited to files saved in Notepad, as long as the opening of the file contains several invisible characters EF BB BF (hexadecimal should be xEFxBBxBF, visible when editing the file in binary). This is like a convention. When the system sees this thing, it will think that your file is UTF-8 encoded.

If your interface is UTF-8, you need to force download a file, such as csv.excel by default (Chinese background), considers csv to be GB encoded, so if there is a BOM header, then you give The file presented by the user may be garbled.

How to add bom?

Just add the BOM header before outputting the file:

The code is as follows:

// 文件名
 $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}");
 // 如果结果中有提示信息,则把第一行输出改为提示信息文字
 $out = "xEFxBBxBF";// 加上bom头,系统自动默认为UTF-8编码
 if (!empty($extra['notice'])) {
 $out .= "{$extra['notice']}rn";
 }
 // 输出
 foreach ($table as $row) {
 $out .= implode(",", $row) . "rn";
 }
 /* if (mb_detect_encoding()($out) == 'UTF-8') {
 $out = iconv("UTF-8//IGNORE", "GBK", $out);
 } */
 echo $out; 
Copy after login

The following content will introduce to you the explanation of the BOM header and how to remove it

The simple way to remove the BOM header is as follows Two types:

1. Method of removing BOM header with editplus

After the editor is adjusted to UTF8 encoding format, there will be a string of hidden characters (that is, BOM) in front of the saved file for editor identification. Whether this file is encoded in UTF8.

Run Editplus, click Tools, select Preferences, select the file, and select the UTF-8 identifier. Always delete the signature, then edit and save the PHP file. The PHP file will not have a BOM.

2. How to remove the BOM header in ultraedit

After opening the file, select the encoding format in the save as option (utf-8 without BOM header), confirm and it’s ok

How about it, removing the BOM header is very simple, right?

Let’s talk about the BOM information of utf8 again

BOM refers to the storage method of the PHP file itself as UTF-8 with BOM. The Chinese garbled way of ordinary pages is generally not caused by this reason.

header("Content-type: text/html; charset=utf-8");
Copy after login

This sentence controls the encoding method of the html output page. The BOM is only available when "Notepad" is used to store it as UTF-8 under WINDOWS. You can use WINHEX to delete the first 2 bytes. .

You can set whether to include BOM in the encoding settings in Dreamweaver. Generally, as long as the output of php is not a picture (GDI Stream), BOM will not cause problems.

GDI Stream will be displayed as a red cross if there are extra characters at the beginning.

The above content introduces how PHP automatically adds BOM headers when downloading files and explains how to remove BOM headers. I hope you like it.

The above introduces how to automatically add the BOM header when downloading files in PHP and how to explain the BOM header and remove the BOM header, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template