您好,欢迎访问一九零五行业门户网

THinkPHP中文件上载

thinkphp中文件下载
thinkphp1.5中文件的下载 用到的系统类库文件是http.class.php,位于thinkphp\lib\org\net目录下,类名http,其中有静态方法static function download ($filename, $showname=”,$content=”,$expire=180);/ @param string $filename 下载文件名(完整路径加文件的保存名字)* @param string $showname 下载显示的文件名(想要显示的名字或者从数据库中读出的原来带中文的名字);* @param string $content 下载的内容(默认为空,此时下载的文件就是原文件)。* @param integer $expire 下载内容浏览器缓存时间 ,默认为空时为180秒。*/因为php保存文件名不支持中文,所以通常中文文件名保存到服务器上时换成成英文名或者生成随机名字。下载时可以利用此方法回复原文件名。应用举例:下载时显示文件原名/* 假设数据库里文件信息存储表为file(id,truename,savenane,user,size).文件存在于网站项目目录下的uploads文件夹里,本网站项目名为bm,其绝对路径为:h:\appserv\www\bm\uploads\( h:\appserv\www\为文档根目录)此时该目录下有一文件123456789.doc,(savename),原文件名为“读后感.doc”,即truename,大小为2mb.那么要下载时服务器端得程序为:class fileaction extends action{public function download(){$uploadpath=’h:\appserv\www\bm\uploads\’;//设置文件上传路径,服务器上的绝对路径$id=$_get['id'];//get方式传到此方法中的参数id,即文件在数据库里的保存id.根据之查找文件信息。if($id==”) //如果id为空而出错时,程序跳转到项目的index/index页面。或可做其他处理。{$this->redirect(‘index’,'index’,”,app_name,”,1);}$file=d(‘file’);//利用与表file对应的数据模型类filemodel来建立数据对象。$result= $file->find($id);//根据id查询到文件信息if($result==false) //如果查询不到文件信息而出错时,程序跳转到项目的index/index页面。或可做其他处理{$this->redirect(‘index’,'index’,”,app_name,”,1);}$savename=$file->savename;//文件保存名$showname=$file->truename;//文件原名$filename=$uploadpath.$savename;//完整文件名(路径加名字)import(‘org.net.http’);http::download($filename,$showname);}}然后在该文件下载的html模板里要下载该文件的地方加一个下载链接,调用file模块的download方法即可。记得传参数id .如本例中:读后感.doc sunmoon 下载
注:ie浏览器的下载文件名编码只有gb2312才能显示,若是不然,要不就是文件名乱码,要不就是找不到文件而无法下载。针对此种情况,我对原来的download()方法进行了一些调整,经过测试发现ie、傲游、firefox均可正常下载。 /** +---------------------- * 下载文件 * 可以指定下载显示的文件名,并自动发送相应的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(file_exists($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(content-type:text/html; charset=utf-8); 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); //下面一行就是改动的地方,即用iconv(utf-8,gb2312//translit,$showname)系统函数转换编码为gb2312 header(content-disposition: attachment; filename=. iconv(utf-8,gb2312,$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为php系统函数库,但需要安装。若是服务器还没有这个模块安装,则需将iconv.dll下载下来后复制到windows/system32/下面,同时在php安装文件夹得ext文件夹里也复制一份。然后在php.ini文件中将extension=php_iconv.dll前的”;”去掉,没有的话就加上extension=php_iconv.dll。然后重启服务器即可。
?
其它类似信息

推荐信息