web页面生成TXT文件供另存为下载

目标,要兼容所有浏览器,让文件名和文件内的中英显示正常。

首先,文件下载,肯定要有个文件名$filename

$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
 


接着,兼容浏览器的文件头

// application/octet-stream为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 . '"');
}
 

然后把内容echo到TXT内容里面就行了。

你可能感兴趣的:(Web,浏览器,firefox)