Home > Backend Development > PHP Tutorial > PHP生成TXT资料

PHP生成TXT资料

WBOY
Release: 2016-06-13 13:02:56
Original
1125 people have browsed it

PHP生成TXT文件

访问PHP的时候生成TXT并自动下载。

?

第一步:处理中文文件名:

$ua = $_SERVER["HTTP_USER_AGENT"];
$filename = "中文文件名.txt";
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
Copy after login

以上方法可支持下载中文文件名。

?

第二步:生成TXT文件

header("Content-Type: application/octet-stream");
if (preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']) ) {
	header('Content-Disposition:  attachment; filename="' . $encoded_filename . '"');
} elseif (preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT'])) {
	header('Content-Disposition: attachment; filename*="utf8' .  $filename . '"');
} else {
	header('Content-Disposition: attachment; filename="' .  $filename . '"');
}
Copy after login

支持各种浏览器。

?

第二步:输出内容

直接用echo输出,“\r\n”用以换行。

?

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