php做下载文件的实现代码及文件名中乱码解决方法,需要的朋友可以参考下。
最近有人问我做下载文件的方法,对于php方法如下:
复制代码 代码如下:
立即学习“PHP免费学习笔记(深入)”;
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=ins.jpg");
readfile("imgs/test_Zoom.jpg");
?>
复制代码 代码如下:
立即学习“PHP免费学习笔记(深入)”;
$filename = "document.txt";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename);
print "Hello!";
?>
复制代码 代码如下:
立即学习“PHP免费学习笔记(深入)”;
$filename = "中文 文件名.txt";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename);
print "Hello!";
?>
复制代码 代码如下:
立即学习“PHP免费学习笔记(深入)”;
Content-Disposition: attachment; filename*="utf8''%E4%B8%AD%E6%96%87%20%E6%96%87%E4%BB%B6%E5%90%8D.txt"
复制代码 代码如下:
立即学习“PHP免费学习笔记(深入)”;
$ua = $_SERVER["HTTP_USER_AGENT"];
$filename = "中文 文件名.txt";
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
header('Content-Type: application/octet-stream');
if (preg_match("/MSIE/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
print 'ABC';
?>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号