php打包下载文件
2015-09-22 10:21
681 查看
使用前请先开启:查看下php.ini里面的extension=php_zip.dll前面的分号有没有去掉;
下载文件
$zip=new \ZipArchive(); $zifile = 'download/' . $bookid . '.zip';//压缩包名字 if($zip->open($zifile, \ZipArchive::OVERWRITE)=== TRUE){ $this->addFileToZip('download/'.$bookname, $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法 $zip->close(); //关闭处理的zip文件 } //添加文件方法 function addFileToZip($path,$zip){ //echo $path; $handler=opendir($path); //打开当前文件夹由$path指定。 while(($filename=readdir($handler))!==false){ //var_dump($filename);continue; if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..’,不要对他们进行操作 if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归 $this->addFileToZip($path."/".$filename, $zip); }else{ //将文件加入zip对象 //echo $path."/".$filename.'<br>'; $a=substr($path."/".$filename,strlen('download/')); //$zip->addFile($path."/".$filename); $zip->addFile($path."/".$filename,$a); } } } @closedir($path); }
下载文件
$this->download ($zifile,' '.$bookname . '.zip'); /** * 可以指定下载显示的文件名,并自动发送相应的Header信息 * 如果指定了content参数,则下载该参数的内容 * @static * @access protected * @param string $filename 下载文件名 * @param string $showname 下载显示的文件名 * @param integer $expire 下载内容浏览器缓存时间 * @return void * @throws ThinkExecption */ protected function download ($filename, $showname='',$expire=180) { if(file_exists($filename)){ $length = filesize($filename); }elseif(is_file(UPLOAD_PATH.$filename)){ $filename = UPLOAD_PATH.$filename; $length = filesize($filename); }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"; } ob_end_clean(); //发送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" ); ob_clean(); readfile($filename); //exit(); }
相关文章推荐
- ThinkPHP中redirect用法分析
- php正则表达式完全教程之精华篇
- PHP 捕捉错误,记录到日志
- PHP 捕捉错误,记录到日志
- php 文件断点续传
- php+mysql新无线级分类
- php安装libiconv-1.14.tar.gz遇到的问题
- thinkphp的目录常量在入口文件index.php里定义
- 【ThinkPHP】验证码
- (转)PHP下编码转换函数mb_convert_encoding与iconv的使用说明
- (转)PHP中构造函数和析构函数解析
- Thinkphp修改默认模板引擎为smarty如何操作
- PHP基础知识
- Lnmp修改php.ini配置以及使exec()函数可用
- php 获取域名的whois 信息
- 【ThinkPHP】关于URL的设置、伪静态
- ThinkPHP-分页效果及自定义类文件引入框架
- php的header函数之设置content-type
- PHP实现搜索相似图片
- php json格式存取数据实例,直接可放php项目直接运行