PHP 下载文件时如何自动添加bom头及解释BOM头和去掉bom头的方法_PHP
PHP 下载文件时自动添加bom头,首先需要大家弄清楚,什么是bom头?在Windows下用记事本之类的程序将文本文件保存为UTF-8格式时,记事本会在文件头前面加上几个不可见的字符(EF BB BF),就是所谓的BOM(Byte order Mark)。
在utf-8编码文件中BOM在文件头部,占用三个字节,用来标示该文件属于utf-8编码,现在已经有很多软件识别bom头,但是还有些不能识别bom头,比如PHP就不能识别bom头,这也是用记事本编辑utf-8编码后执行就会出错的原因了。
不仅限于 记事本保存的文件,只要在文件的开口包含了EF BB BF 几个不可见的字符(十六进制应该是是xEFxBBxBF,用二进制编辑文件可见)。这像是一个约定俗成的东西,当系统看到这玩意的时候,就会觉得你这个文件是UTF-8编码的。
如果你的接口是UTF-8的,你需要强制下载一个文件,比如csv.excel在默认情况(中文背景)下,认为csv是GB编码的,所以如果米有bom头,那你给用户呈现的文件,可能就是乱码了。
怎么加bom头呢?
在输出文件之前加上bom头就可以了:
代码如下:
// 文件名 $filename = "www.bitsCN.com .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;
以下内容给大家介绍解释BOM头和去掉方法
去掉bom头的办法,简单的是下面两种:
1、editplus去BOM头的方法
编辑器调整为UTF8编码格式后,保存的文件前面会多出一串隐藏的字符(也即是BOM),用于编辑器识别这个文件是否是以UTF8编码。
运行Editplus,点击工具,选择首选项,选中文件,UTF-8标识选择 总是删除签名,然后对PHP文件编辑和保存后的PHP文件就是不带BOM的了。
2、ultraedit去除bom头办法
打开文件后,另存为选项的编码格式里选择(utf-8 无bom头),确定就ok了
怎么样,去掉bom头很简单吧
再来一段议论utf8的BOM信息的
BOM是指php文件本身的存储方式为带BOM的UTF-8,普通页面的中文乱码方式一般不是由这个原因导致的。
header("Content-type: text/html; charset=utf-8");
这句话控制html输出页面的编码方式,BOM只有在WINDOWS下采用“记事本”存储为UTF-8时才会有,这个可以用WINHEX把开始的2个字节删掉。
在dreamweaver里面编码设置里面可以设置是否带BOM,一般只要php输出的不是图片(GDI Stream),BOM都不会导致问题。
GDI Stream如果开头有了额外的 字符就会显示为 红叉。
以上内容给大家介绍了PHP 下载文件时自动添加bom头及解释BOM头和去掉方法,希望大家喜欢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
