时间:2021-07-01 10:21:17 帮助过:26人阅读
- <br><!--?php <BR-->header("Content-Type: application/force-download"); <br>header("Content-Disposition: attachment; filename=ins.jpg"); <br>readfile("imgs/test_Zoom.jpg"); <br>?> <br> <br>第一行代码是强制下载; <br>第二行代码是给下载的内容指定一个名字; <br>第三行代码是把下载的内容读进文件中。 <br><strong>如何在PHP下载文件名中解决乱码 <br></strong>通过把Content-Type设置为application/octet-stream,可以把动态生成的内容当作文件来下载,相信这个大家都会。那么用Content-Disposition设置下载的文件名,这个也有不少人知道吧。基本上,下载程序都是这么写的: <br><span style="CURSOR: pointer" onclick="doCopy('code27735')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->$filename = "document.txt"; <br>header('Content-Type: application/octet-stream'); <br>header('Content-Disposition: attachment; filename=' . $filename); <br>print "Hello!"; <br>?> <br> <br>这样用浏览器打开之后,就可以下载document.txt。 <br>但是,如果$filename是UTF-8编码的,有些浏览器就无法正常处理了。比如把上面那个程序稍稍改一下: <br><span style="CURSOR: pointer" onclick="doCopy('code5708')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->$filename = "中文 文件名.txt"; <br>header('Content-Type: application/octet-stream'); <br>header('Content-Disposition: attachment; filename=' . $filename); <br>print "Hello!"; <br>?> <br> <br>把程序保存成UTF-8编码再访问,IE6下载的文件名就会乱码。 FF3下下载的文件名就只有“中文”两个字。Opera 9下一切正常。 <br></li></ol></pre>输出的header实际上是这样子: <br>Content-Disposition: attachment; filename=中文 文件名.txt其实按照RFC2231的定义,多语言编码的Content-Disposition应该这么定义: <br><span style="CURSOR: pointer" onclick="doCopy('code45229')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>Content-Disposition: attachment; filename*="utf8''%E4%B8%AD%E6%96%87%20%E6%96%87%E4%BB%B6%E5%90%8D.txt" <br> <br>即: <br>filename后面的等号之前要加 * <br>filename的值用单引号分成三段,分别是字符集(utf8)、语言(空)和urlencode过的文件名。 <br>最好加上双引号,否则文件名中空格后面的部分在Firefox中显示不出来 <br>注意urlencode的结果与php的urlencode函数结果不太相同,php的urlencode会把空格替换成+,而这里需要替换成%20 <br>经过试验,发现几种主流浏览器的支持情况如下: <br>IE6 attachment; filename="<url编码之后的utf-8文件名>" <br>FF3 attachment; filename="UTF-8文件名" <br>attachment; filename*="utf8''<url编码之后的utf-8文件名>" <br>O9 attachment; filename="UTF-8文件名" <br>Safari3(Win) 貌似不支持?上述方法都不行 <br>这样看来,程序必须得这样写才能支持所有主流浏览器: <br><span style="CURSOR: pointer" onclick="doCopy('code26666')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->$ua = $_SERVER["HTTP_USER_AGENT"]; <br>$filename = "中文 文件名.txt"; <br>$encoded_filename = urlencode($filename); <br>$encoded_filename = str_replace("+", "%20", $encoded_filename); <br>header('Content-Type: application/octet-stream'); <br>if (preg_match("/MSIE/", $ua)) { <br>header('Content-Disposition: attachment; filename="' . $encoded_filename . '"'); <br>} else if (preg_match("/Firefox/", $ua)) { <br>header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"'); <br>} else { <br>header('Content-Disposition: attachment; filename="' . $filename . '"'); <br>} <br>print 'ABC'; <br>?> <br></li><li><p></p></li><li><p align="left"><span id="url" itemprop="url">http://www.bkjia.com/PHPjc/322984.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/322984.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">最近有人问我做下载文件的方法,对于php方法如下: 代码如下:</span></p><pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li>?php header("Content-Type: application/force-download"); header("Content-Dispositio...<p></p></li><li> </li></ol></pre></li></ol></pre></url编码之后的utf-8文件名></url编码之后的utf-8文件名></li></ol></pre></li></ol></pre>