访问PHP时生成TXT文件并自动下载。
$filename = "filename.txt"; header("Content-Type: application/octet-stream"); header(‘Content-Disposition: attachment; filename="‘ . $filename . ‘"‘);
输出内容:
直接用echo输出,“\r\n”用以换行。
第一步:处理中文文件名:
$ua = $_SERVER["HTTP_USER_AGENT"]; $filename = "中文文件名.txt"; $encoded_filename = urlencode($filename); $encoded_filename = str_replace("+", "%20", $encoded_filename);
第二步:生成TXT文件:
1 header("Content-Type: application/octet-stream"); 2 if (preg_match("/MSIE/", $_SERVER[‘HTTP_USER_AGENT‘]) ) { 3 header(‘Content-Disposition: attachment; filename="‘ . $encoded_filename . ‘"‘); 4 } elseif (preg_match("/Firefox/", $_SERVER[‘HTTP_USER_AGENT‘])) { 5 header(‘Content-Disposition: attachment; filename*="utf8‘ . $filename . ‘"‘); 6 } else { 7 header(‘Content-Disposition: attachment; filename="‘ . $filename . ‘"‘); 8 }
第三步:输出内容:
直接用echo输出,“\r\n”用以换行。
时间: 2024-10-04 11:17:03