当前位置: 首页 > news >正文

Thinkphp 多文件压缩

控制器

<?php

namespace app\api\controller;
use think\Controller;
use think\facade\Db;
use think\facade\Request;

use ZipArchive;

class DrugTestResult
{
    public function download(){

        if(Request::isPost()){
            
            $data = Request::post();
            
            $idnumber = Request::param('idnumber');
            $info = Db::name('drug_test_result')->where('idnumber',$idnumber)->where('is_delete',1)->find();
            
            if($info){
                
                // 创建压缩临时目录
                mkdir('download/'.$idnumber, 0777, true);
                
                // 压缩的文件夹路径
                $folderPath = 'download/'.$idnumber;
            
                // 压缩后的 ZIP 文件路径
                $zipFilePath = 'download/'.$idnumber.'.zip';
            
                // 创建 ZipArchive 实例
                $zip = new ZipArchive();
            
                if ($zip->open($zipFilePath, ZipArchive::CREATE) === true) {
                    
                    //查询压缩的文件列表
                    $images = Db::name('drugimg')->where('id','in',$info['image'])->select()->ToArray();
                    
                    foreach ($images as $k => $v){
                        
                        #源文件夹
                        $fileDir = '/www/wwwroot/abcapi/public/'.$v['thumb'];   
                        
                        #目标文件夹
                        $tarDir = '/www/wwwroot/abcapi/public/download/'.$idnumber.'/'.$v['title'];   
                        //复制到临时文件夹
                        copy($fileDir,$tarDir);
                    }
                    
                    $files = scandir($folderPath);
                
                    foreach ($files as $file) {
                        // 排除当前目录和上级目录
                        if ($file != '.' && $file != '..') {
                            $filePath = $folderPath . '/' . $file;
                            $relativePath = basename($file);
                            $zip->addFile($filePath);
                        }
                    }
                    
                    $zip->close();
                    
                    //删除临时文件夹
                    deleteFolder('/www/wwwroot/abcapi/public/'.$folderPath.'/');
                    
                    //返回压缩包地址(接口)
                    echo apireturn(200,200,'success','/'.$zipFilePath);
                    die;
                    
                    // 设置响应头,告诉浏览器下载文件
                    // header("Content-Type: application/zip");
                    // header("Content-Disposition: attachment; filename=\"" . basename($zipFilePath) . "\"");
                    // header("Content-Length: " . filesize($zipFilePath));
            
                    // 读取并输出压缩文件内容
                    //readfile($zipFilePath);
                    
                    // 删除临时 ZIP 文件
                    //unlink($zipFilePath);
            
                } else {
                    echo apireturn(200,201,'faild','');die;
                    
                }
            }else{
                echo apireturn(200,201,'文件不存在或已被删除','');
                die;
            }
        }
    }
}

这里我采用的是php自带的ZipArchive类
需要先确认php.ini配置文件中是否取消注释了extension=zip和extension=openssl 来启用这些扩展

公共函数

/**
 * 通用化API输出格式
 * @param  integer  $status   http状态码
 * @param  string  $code  业务状态码
 * @param  string  $msg  消息内容
 * @param  array   $data     数据
 * @return json            返回json格式的数据
 */
function apireturn($status,$code,$msg,$data){
    $data_rt['status'] = $status;
    $data_rt['code'] = $code;
    $data_rt['msg'] = $msg;
    if(is_array($data)){
        if(!empty($data)){
            $data_rt['data'] = $data;
        }else{
            $data_rt['data'] = array();
        }
    }else{
        if(!empty($data)){
            $data_rt['data'] = $data;
        }else{
            $data_rt['data'] = array();
        }
    }
    return json_encode($data_rt,TRUE);
}

//删除文件
function deleteFolder($folderPath) {
    if (is_dir($folderPath)) {
        if (is_dir_empty($folderPath)) {
            rmdir($folderPath);
        } else {
            // 文件夹不为空,先删除其中所有文件和文件夹
            $files = scandir($folderPath);
            foreach ($files as $file) {
                if ($file != "." && $file != "..") {
                    $filePath = $folderPath . "/" . $file;
                    if (is_dir($filePath)) {
                        deleteFolder($filePath);
                    } else {
                        unlink($filePath);
                    }
                }
            }
            // 删除完子文件和子文件夹后再删除空文件夹
            rmdir($folderPath);
        }
    }
}
 
function is_dir_empty($folderPath) {
    $files = scandir($folderPath);
    return count($files) <= 2;
}

相关文章:

  • Java 大视界 -- 基于 Java 的大数据机器学习模型的多模态融合技术与应用(143)
  • Android Compose 图标按钮深度剖析:从源码到实践(五)
  • MES汽车零部件制造生产监控看板大屏
  • 【Go 】异常处理
  • SpringBoot第四站(1):数据层开发: 配置数据源,整合jdbcTemplate
  • Linux文件挂载新文件夹,隐藏老文件夹问题
  • 【React】useEffect、useLayoutEffect底层机制
  • docker desktop 集成WSL Ubuntu22.04
  • FPGA----完美解决Windows下[XSIM 43-3409][XSIM 43-3915]错误
  • 自定义myshell(精讲)
  • 服务器相关的硬件知识
  • Ae 效果详解:描边
  • 空调遥控器低功耗单片机方案
  • Qt6+QML实现Windows屏幕录制
  • 云原生网络拓扑:服务网格的量子纠缠效应
  • Agilent N5230A矢量网络分析仪测试散射参数(S参数)步骤
  • RabbitMQ实现定时/延迟任务
  • 力扣 Hot 100 刷题记录 - 二叉搜索树中第 K 小的元素
  • Rust与Java对比
  • 【redis】AOF 的基本工作机制,顺序写入,文件同步,重写机制
  • 美国恶劣天气已造成至少28人死亡
  • 古稀之年的设计家吴国欣:重拾水彩,触摸老上海文脉
  • 大风+暴雨,中央气象台双预警齐发
  • 上海交大曾小勤:科技传播不应停留于知识搬运,要做科学思维的播种机
  • 流失79载,国宝文物“子弹库帛书”(二、三卷)回归祖国
  • 澎湃与七猫联合启动百万奖金征文,赋能非虚构与现实题材创作