Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add bomber headers automatically when downloading files by PHP

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains how PHP can automatically add bomber headers when downloading files. Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how PHP can automatically add bomb headers when downloading files.

First of all, let's get this straight. What is a bom header? When you use a program such as notepad to save a text file in UTF-8 format under Windows, notepad adds a few invisible characters (EF BB BF) in front of the header, which is called BOM (Byte order Mark).

It is not limited to files saved in notepad, as long as the opening of the file contains a few invisible characters of EF BB BF (hexadecimal should be xEFxBBxBF, visible in binary editing files). It's like a convention, and when the system sees it, it thinks your file is UTF-8-coded.

If your interface is UTF-8, you need to download a file forcefully. For example, by default (Chinese background), csv.excel thinks that csv is encoded by GB, so if rice has a bom header, the file you present to the user may be garbled.

How to add bom head?

Just add the bom header before the output file:

The copy code is as follows:

/ / File name

$filename = "www.jb51.net.net.csv"

Header ('Expires:'. Gmdate ('D, d M Y REQUEST_TIME', $_ 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 prompt text

$out = "xEFxBBxBF"; / / with the 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

At this point, I believe you have a deeper understanding of "how PHP can automatically add bom headers when downloading files". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report