记php移动并压缩多级目录文件为zip文件并上传oss

话不多说 上代码 实测好用

    public function goods_zip(){
        $goods_id = input('goods_id');
        $root_path = Env::get('root_path').'public';
        $goodszipInfo = db('goods_zipimg')->where('goods_id',$goods_id)->find();
        
 
        // 处理首图
        $fileName = $goods_id.'.png';
        $path_up = $root_path.'/'.$goodszipInfo['goods_thumb'];
        $path_to = $root_path.'/zip_img/'.$goods_id.'/thumb/';
        $res =  copyFile($path_up,$path_to,$fileName);

        // 处理图组
        $goods_imgs = explode(',',$goodszipInfo['goods_imgs']);
        foreach($goods_imgs as $k=>$v){
            $fileName = 'imgs_'.($k+1).'.png';
            $path_up = $root_path.'/'.$v;
            $path_to = $root_path.'/zip_img/'.$goods_id.'/imgs/';    
            $res =  copyFile($path_up,$path_to,$fileName);
        }
        
        // 处理详情图
        $goods_imgs = explode(',',$goodszipInfo['goods_imgs']);
        foreach($goods_imgs as $k=>$v){
            $fileName = 'detail_'.($k+1).'.png';
            $path_up = $root_path.'/'.$v;
            $path_to = $root_path.'/zip_img/'.$goods_id.'/detail/';    
            $res =  copyFile($path_up,$path_to,$fileName);
        }        
        
        // 压缩文件并上传
        $zipimg_path = $root_path.'/zip_img/'.$goods_id.'/';
        $zipimg_path_to = $root_path.'/zip_img/'.$goods_id.'/';

        $zip_path = $zipimg_path.'zip_'.$goods_id.'.zip';
        $zip_name = 'zip_'.$goods_id.'.zip';
        if(file_exists($zip_path)){
            unlink($zip_path);
        }
        $res = $this->zip($zipimg_path,$zip_path);

        // 压缩成功上传阿里云oss
        if($res){
            $oss_zip =  OssUpload::sendzipimg($zip_path,$zip_name);
            if($oss_zip){
                $his_goods_zip = db('goods')->where('goods_id',$goods_id)->value('goods_zip');
                $res = db('goods')->where('goods_id',$goods_id)->update(['goods_zip'=>$oss_zip]);
                if($res && $his_goods_zip)OssUpload::delimg($his_goods_zip);
                $this->success('图片打包上传成功', 0);  
            }
        }      
        $this->success('失败', 0);  
    } 

下面是压缩多级目录 方法   

网上查了好多   不是压缩包显示目录太多  就是不能压缩多级目录  

    public function zip($dir_path, $zipName)
    { 
        $relationArr = array(
            $dir_path => array(
                'originName' => $dir_path,
                'is_dir' => true,
                'children' => array()
            )
        );
        $this->modifiyFileName($dir_path, $relationArr[$dir_path]['children']);
        $key = array_keys($relationArr);
        $val = array_values($relationArr);
        $zip = new \ZipArchive;
        //ZIPARCHIVE::CREATE没有即是创建
        $zip->open($zipName,\ZipArchive::CREATE);
        $this->zipDir($key[0], '', $zip, $val[0]['children']);
        $zip->close();
        return true;
    }
    /**
     * [zipDir 给zip文件添加目录及文件]
     * @param  [type] $real_path   [根目录]
     * @param  [type] $zip_path    [压缩目录]
     * @param  [type] &$zip        [引入zip资源]
     * @param  [type] $relationArr [参数变量]
     * @return [type]              [description]
     */
    function zipDir($real_path, $zip_path, &$zip, $relationArr)
    {
        $sub_zip_path = empty($zip_path) ? '' : $zip_path . '/';
        if (is_dir($real_path)) {
              foreach ($relationArr as $k => $v) {
                if ($v['is_dir']) {  //是文件夹
                    $zip->addEmptyDir($sub_zip_path . $v['originName']);
                    $this->zipDir($real_path . '/' . $k, $sub_zip_path . $v['originName'], $zip, $v['children']);
                } else { //不是文件夹
                    $zip->addFile($real_path . '/' . $k, $sub_zip_path . $k);
                }
            }
        }
    }
    /**
     * [modifiyFileName 遍历目录下所有文件夹及文件]
     * @param  [type] $path         [需要压缩的目录路径]
     * @param  [type] &$relationArr [引入参数变量]
     * @return [type]               [无]
     */
    function modifiyFileName($path, &$relationArr){
        if (!is_dir($path) || !is_array($relationArr)) {
            return false;
        }
        if ($dh = opendir($path)) {
            $count = 0;
            while (($file = readdir($dh)) !== false) {
                if(in_array($file,array('.', '..', null))) continue; //无效文件,重来
                if (is_dir($path . '/' . $file)) {
                  $relationArr[$file] = array(
                    'originName' => iconv('GBK', 'UTF-8', $file),
                    'is_dir' => true,
                    'children' => array()
                  );
                  $this->modifiyFileName($path . '/' . $file, $relationArr[$file]['children']);
                }else{
                  $relationArr[$file] = array(
                    'originName' => iconv('GBK', 'UTF-8', $file),
                    'is_dir' => false,
                    'children' => array()
                  );
                }
            }
        }
    }

 



posted @ 2020-09-16 17:06  Liiu  阅读(247)  评论(0编辑  收藏  举报