解决php输出文件下载时文件名含中文时出现乱码

解决php输出文件下载时文件名含中文时出现乱码

2011-10-19

接前篇 前篇提到了用php生成网页快捷方式 进行下载 但是却存在一个文件名乱码的问题

php下载时,用一个header可以确定保存为的名字:

header( ‘Content-Disposition: attachment; filename=”‘.$saved_name.’”‘ );//$saved_name存为的名字

但是如果是中文的话,在ie下是乱码。其他浏览器是正常的。

这时候,需要urlencode($saved_name),ie下可以保存为正常的中文名字了。

但是其他浏览器在url编码时候不会自动解码。

这时候就要靠判断浏览器类型了了:

if(strpos($_SERVER['HTTP_USER_AGENT'],”MSIE”))
header( ‘Content-Disposition: attachment; filename=”‘.urlencode($this->saved_name).’”‘ );//如果是ie存为的名字要urlencode
else

header( ‘Content-Disposition: attachment; filename=”‘.$this->saved_name.’”‘ );//存为的名字

这样就可以解决这个问题。

另附一个网上看到的一个输出文件的例子:

header( “Pragma: public” );

header( “Expires: 0″ ); // 一定要设置不缓存哦。

header( “Cache-Component: must-revalidate, post-check=0, pre-check=0″ );

header( “Content-type:”.$mineType );

header( “Content-Length: ” . filesize( $path) );

if(strpos($_SERVER['HTTP_USER_AGENT'],”MSIE”))

header( ‘Content-Disposition: attachment; filename=”‘.urlencode($saved_name).’”‘ );//如果是ie存为的名字要urlencode

else header( ‘Content-Disposition: attachment; filename=”‘.$saved_name.’”‘ );//存为的名字

header( ‘Content-Transfer-Encoding: binary’ );

readfile( $path);//读取并输出

posted @ 2013-05-22 10:36  holyes  阅读(462)  评论(0编辑  收藏  举报