lorry01
一个普通的程序员,在不断的努力~

导航

 

解决PHP下载乱码问题,word doc 文件等

    /**
     +----------------------------------------------------------
     * 下载文件
     * 可以指定下载显示的文件名,并自动发送相应的Header信息
     * 如果指定了参content数,则下载该参数的内容
     +----------------------------------------------------------
     * @static
     * @access public
     +----------------------------------------------------------
     * @param string $filename 下载文件名
     * @param string $showname 下载显示的文件名
     * @param string $content  下载的内容
     * @param integer $expire  下载内容浏览器缓存时间
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    static public function download ($filename, $showname='',$content='',$expire=180) {
        if(is_file($filename)) {
            $length = filesize($filename);
        }elseif(is_file(UPLOAD_PATH.$filename)) {
            $filename = UPLOAD_PATH.$filename;
            $length = filesize($filename);
        }elseif($content != '') {
            $length = strlen($content);
        }else {
            throw_exception($filename.L('下载文件不存在!'));
        }
        if(empty($showname)) {
            $showname = $filename;
        	$showname = basename($showname);
        }
		if(!empty($filename)) {
	        $type = mime_content_type($filename);
		}else{
			$type	 =	 "application/octet-stream";
		}
        //发送Http Header信息 开始下载
        header("Pragma: public");
        header("Cache-control: max-age=".$expire);
        //header('Cache-Control: no-store, no-cache, must-revalidate');
        header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT");
        header("Content-Disposition: attachment; filename=".$showname);
        header("Content-Length: ".$length);
        header("Content-type: ".$type);
        header('Content-Encoding: none');
        header("Content-Transfer-Encoding: binary" );
        if($content == '' ) {
            readfile($filename);
        }else {
        	echo($content);
        }
        exit();
    }

  主要是编码问题,进行下编码转换,加一句代码

iconv('utf-8', 'gb2312', $filename);

 也就是把编码转换成你系统默认的编码, 这个基本可以解决文件名乱码的问题

  但是有时还会有内容乱码问题,这个问题也是编码问题吧

具体可以参考:http://blog.rifun.net/2012-05-02/540.html

广告时间:网购一族   爱皮皮购物 谢谢支持哦!

posted on 2012-05-02 12:03  lorry01  阅读(881)  评论(0)    收藏  举报