日期:2014-05-17  浏览次数:20474 次

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 .
如本例中:
<table>
<tr>
<td>读后感.doc</td>
<td>sunmoon</td>
<td><a href=’__APP__/File/download/id/{$id}’>下载</a></td>
<!–
其中{$id}是模板变量,代表要下载的文件在数据库中的保存id.
–>
</tr>
</table>
 

注: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。然后重启服务器即可。

?