<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pzg&#039;s blog &#187; 乱码</title>
	<atom:link href="http://pzg.me/tag/%e4%b9%b1%e7%a0%81/feed/" rel="self" type="application/rss+xml" />
	<link>http://pzg.me</link>
	<description>漫步……</description>
	<lastBuildDate>Wed, 08 Sep 2010 08:50:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PHP下载文件名乱码问题详解</title>
		<link>http://pzg.me/web/372/download-file-name-garbled-php-explain-problem/</link>
		<comments>http://pzg.me/web/372/download-file-name-garbled-php-explain-problem/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 00:18:49 +0000</pubDate>
		<dc:creator>countmeon</dc:creator>
				<category><![CDATA[网页设计]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[乱码]]></category>
		<category><![CDATA[详解]]></category>

		<guid isPermaLink="false">http://pzg.me/?p=372</guid>
		<description><![CDATA[通过把Content-Type设置为application/octet-stream，可以把动态生成的内容当作文件来下载，相信这个大家都会。那么用Content-Disposition设置下载的文件名，这个也有不少人知道吧。基本上，下载程序都... ]]></description>
			<content:encoded><![CDATA[<p>通过把Content-Type设置为application/octet-stream，可以把动态生成的内容当作文件来下载，相信这个大家都会。那么用Content-Disposition设置下载的文件名，这个也有不少人知道吧。基本上，下载程序都是这么写的：</p>
<p>header(&#8216;Content-Disposition: attachment; filename=&#8217; . $filename);print &#8220;Hello!&#8221;;?&gt; </p>
<p>这样用浏览器打开之后，就可以下载document.txt。</p>
<p>但是，如果$filename是UTF-8编码的，有些浏览器就无法正常处理了。比如把上面那个程序稍稍改一下：</p>
<p>header(&#8216;Content-Disposition: attachment; filename=&#8217; . $filename);print &#8220;Hello!&#8221;;?&gt;  </p>
<p>把程序保存成UTF-8编码再访问，IE6下载的文件名就会乱码。 FF3下下载的文件名就只有“中文”两个字。Opera 9下一切正常。</p>
<p>输出的header实际上是这样子：</p>
<p>Content-Disposition: attachment; filename=中文 文件名.txt<br />
其实按照RFC2231的定义，多语言编码的Content-Disposition应该这么定义：</p>
<p>Content-Disposition: attachment; filename*=&#8221;utf8&#8221;%E4%B8%AD%E6%96%87%20%E6%96%87%E4%BB%B6%E5%90%8D.txt&#8221;</p>
<p>即：</p>
<p>filename后面的等号之前要加 * filename的值用单引号分成三段，分别是字符集(utf8)、语言(空)和urlencode过的文件名。 最好加上双引号，否则文件名中空格后面的部分在Firefox中显示不出来 注意urlencode的结果与php的urlencode函数结果不太相同，php的urlencode会把空格替换成+，而这里需要替换成%20</p>
<p>经过试验，发现几种主流浏览器的支持情况如下：</p>
<p>IE6 attachment; filename=&#8221;"</p>
<p>FF3 attachment; filename=&#8221;UTF-8文件名&#8221;</p>
<p>attachment; filename*=&#8221;utf8&#8221;&#8221;</p>
<p>O9 attachment; filename=&#8221;UTF-8文件名&#8221;</p>
<p>Safari3(Win) 貌似不支持？上述方法都不行</p>
<p>这样看来，程序必须得这样写才能支持所有主流浏览器：</p>
<p>以下为引用的内容：</p>
<p>$encoded_filename = urlencode($filename);$encoded_filename = str_replace(&#8220;+&#8221;, &#8220;%20&#8243;, $encoded_filename);header(&#8216;Content-Type: application/octet-stream&#8217;);if (preg_match(&#8220;/MSIE/&#8221;, $ua)) { header(&#8216;Content-Disposition: attachment; filename=&#8221;&#8216; . $encoded_filename . &#8216;&#8221;&#8216;);} else if (preg_match(&#8220;/Firefox/&#8221;, $ua)) { header(&#8216;Content-Disposition: attachment; filename*=&#8221;utf8\&#8217;\&#8221; . $filename . &#8216;&#8221;&#8216;);} else { header(&#8216;Content-Disposition: attachment; filename=&#8221;&#8216; . $filename . &#8216;&#8221;&#8216;);}print &#8216;ABC&#8217;;?&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://pzg.me/web/372/download-file-name-garbled-php-explain-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP的编码问题</title>
		<link>http://pzg.me/web/181/php-coding-problems/</link>
		<comments>http://pzg.me/web/181/php-coding-problems/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 07:33:07 +0000</pubDate>
		<dc:creator>countmeon</dc:creator>
				<category><![CDATA[网页设计]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[乱码]]></category>
		<category><![CDATA[编码]]></category>

		<guid isPermaLink="false">http://pzg.me/?p=181</guid>
		<description><![CDATA[  刚开始学PHP很容易出现乱码情况，一般是因为编码不一致导致的。碰到乱码情况主要考虑三个方面：   页面存储的编码，PHP处理数据的编码，MYSQL的数据编码。   比如页面&#60;meta http-equiv=&#82... ]]></description>
			<content:encoded><![CDATA[<p>  刚开始学PHP很容易出现乱码情况，一般是因为编码不一致导致的。碰到乱码情况主要考虑三个方面：</p>
<p>  页面存储的编码，PHP处理数据的编码，MYSQL的数据编码。</p>
<p>  比如页面&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text/html; charset=UTF-8&#8243; /&gt;  设定了UTF-8的编码 PHP在操作数据库的时候也设置下mysql_query(&#8220;set names &#8216;utf8&#8242;&#8221;); 最后就是把数据库的编码设置成utf8_general_ci  统一了编码就会少很多问题。</p>
]]></content:encoded>
			<wfw:commentRss>http://pzg.me/web/181/php-coding-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
