压缩服务器中的文件夹,并下载到电脑

1.先把文件保存到服务器上传的文件夹中

例子:

1 public function downPicFile($url,$filename,$pic_name){
2         $file_path='./CUSTOM/'.$filename;//地址
3         $path = $file_path.'/'.$pic_name;//地址对应的图片名称
4         if(!file_exists($file_path)){
5             @mkdir($file_path,0777);
6         }
7         $data = file_get_contents($url);//获取图片
8         @file_put_contents($path, $data);//保存图片
9     }
View Code

2.thinkphp的类 FileToZip.class.php

  1 <?php
  2 /**
  3  * zip下载类文件
  4  * 遍历目录,打包成zip格式
  5  */
  6 class traverseDir{
  7     public $currentdir;//当前目录
  8     public $filename;//文件名
  9     public $fileinfo;//用于保存当前目录下的所有文件名和目录名以及文件大小
 10     public $savepath;
 11     public function __construct($curpath,$savepath){
 12         $this->currentdir=$curpath;//返回当前目录
 13         $this->savepath=$savepath;//返回当前目录
 14     }
 15     //遍历目录
 16     public function scandir($filepath){
 17         if (is_dir($filepath)){
 18             $arr=scandir($filepath);
 19             foreach ($arr as $k=>$v){
 20                 $this->fileinfo[$v][]=$this->getfilesize($v);
 21             }
 22         }else {
 23             echo "<script>alert('当前目录不是有效目录');</script>";
 24         }
 25     }
 26     /**
 27      * 返回文件的大小
 28      *
 29      * @param string $filename 文件名
 30      * @return 文件大小(KB)
 31      */
 32     public function getfilesize($fname){
 33         return filesize($fname)/1024;
 34     }
 35     /**
 36      * 压缩文件(zip格式)
 37      */
 38     public function tozip($items){
 39         $zip=new ZipArchive();
 40         $zipname=date('Y-m-d');
 41         if (!file_exists($zipname)){
 42             $zip->open($savepath.$zipname.'.zip',ZipArchive::OVERWRITE);//创建一个空的zip文件
 43             for ($i=0;$i<count($items);$i++){
 44                 $zip->addFile($this->currentdir.'/'.$items[$i],$items[$i]);
 45             }
 46             $zip->close();
 47             $dw=new download($zipname.'.zip',$savepath); //下载文件
 48             $dw->getfiles();
 49             unlink($savepath.$zipname.'.zip'); //下载完成后要进行删除
 50         }
 51     }
 52 }
 53 /**
 54  * 下载文件
 55  *
 56  */
 57 class download{
 58     protected $_filename;
 59     protected $_filepath;
 60     protected $_filesize;//文件大小
 61     protected $savepath;//文件大小
 62     public function __construct($filename,$savepath){
 63         $this->_filename=$filename;
 64         $this->_filepath=$savepath.$filename;
 65     }
 66     //获取文件名
 67     public function getfilename(){
 68         return $this->_filename;
 69     }
 70     //获取文件路径(包含文件名)
 71     public function getfilepath(){
 72         return $this->_filepath;
 73     }
 74     //获取文件大小
 75     public function getfilesize(){
 76         return $this->_filesize=number_format(filesize($this->_filepath)/(1024*1024),2);//去小数点后两位
 77     }
 78     //下载文件的功能
 79     public function getfiles(){
 80         //检查文件是否存在
 81         if (file_exists($this->_filepath)){
 82             //打开文件
 83             $file = fopen($this->_filepath,"r");
 84             //返回的文件类型
 85             Header("Content-type: application/octet-stream");
 86             //按照字节大小返回
 87             Header("Accept-Ranges: bytes");
 88             //返回文件的大小
 89             Header("Accept-Length: ".filesize($this->_filepath));
 90             //这里对客户端的弹出对话框,对应的文件名
 91             Header("Content-Disposition: attachment; filename=".$this->_filename);
 92             //修改之前,一次性将数据传输给客户端
 93             echo fread($file, filesize($this->_filepath));
 94             //修改之后,一次只传输1024个字节的数据给客户端
 95             //向客户端回送数据
 96             $buffer=1024;//
 97             //判断文件是否读完
 98             while (!feof($file)) {
 99                 //将文件读入内存
100                 $file_data=fread($file,$buffer);
101                 //每次向客户端回送1024个字节的数据
102                 echo $file_data;
103             }
104             fclose($file);
105         }else {
106             echo "<script>alert('对不起,您要下载的文件不存在');</script>";
107         }
108     }
109 }
View Code

3.调用类文件打包文件夹

 1 public function downZip($file){
 2         $cur_file = './CUSTOM/'.$file;
 3         $save_path = './CUSTOM/'.$file;
 4         import('Vendor.FileZip.FileToZip',LIB_PATH,'.class.php');
 5         
 6         // 打包下载
 7         $handler = opendir($cur_file); //$cur_file 文件所在目录
 8         $download_file = array();
 9         $i = 0;
10         while( ($filename = readdir($handler)) !== false ) {
11             if($filename != '.' && $filename != '..') {
12                 $download_file[$i++] = $filename;
13             }
14         }
15         closedir($handler);
16         $scandir=new \traverseDir($cur_file,$save_path); //$save_path zip包文件目录
17         $scandir->tozip($download_file);
18     }
View Code

 4.删除文件夹及文件

 1 public function deldir($filename){
 2         $dir = './images/'.$filename;
 3         if(file_exists($dir)){
 4             $dh=opendir($dir);//先删除目录下的文件:
 5             while ($file=readdir($dh)) {
 6                 if($file!="." && $file!="..") {
 7                     $fullpath=$dir."/".$file;
 8                     if(!is_dir($fullpath)) {
 9                         unlink($fullpath);
10                     } else {
11                         deldir($fullpath);
12                     }
13                 }
14             }
15             closedir($dh);
16             //删除当前文件夹:
17             if(rmdir($dir)) {
18                 return true;
19             } else {
20                 return false;
21             }
22         }
23     }

 5.

$ch = curl_init(); //初始化CURL句柄
            curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0 );//可延迟等待时间10
            curl_setopt($ch, CURLOPT_NOSIGNAL, 1);//使用毫秒计时
            curl_setopt($ch, CURLOPT_TIMEOUT_MS , 200000 );//超时时间100,测试延迟改为1
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); //设置请求方式
            curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeaders);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
            $resultjson = curl_exec($ch);//执行预定义的CURL
            curl_close($ch);
    
            $arr = json_decode($resultjson,true);
View Code

 

转载于:https://www.cnblogs.com/heyang71212/p/4633438.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值