• 解决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);//读取并输出

  • 相关阅读:
    XML 树操作 语法
    重构:第一个案例
    敏捷开发 慨叙
    sql server 字段类型
    Linux Win7双系统安装
    IE6/7不读取CSS样式,或不能正常显示背景图片问题
    eclipse 添加字体
    eclipse中测试Hibernate异常报 ORA00926: 缺失 VALUES 关键字
    关于简历的想法几点
    eclipse中测试Hibernate异常报'hibernate.dialect' must be set when no Connection avalable
  • 原文地址:https://www.cnblogs.com/holyes/p/da050f03ffb03e6913806567e9f1e824.html
Copyright © 2020-2023  润新知