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

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;
}


http://www.kler.cn/a/596114.html

相关文章:

  • LeetCode 2517礼盒的最大甜蜜度
  • 嵌入式面经(2)——央企篇
  • 嵌入式C语言进阶(四)查漏补缺
  • MATLAB 实现 Chatterjee 相关系数矩阵计算与特征选择
  • 银联无感支付实现
  • 对接豆包大模型
  • 机器学习——分类、回归、聚类、LASSO回归、Ridge回归(自用)
  • 深入理解 Redis SDS:高效字符串存储的秘密
  • PostgreSQL 常用函数 覆盖九成场景
  • GitHub Copilot Extensions:解锁开发者新能力
  • 人工智能之数学基础:线性方程组求解的得力助手——增广矩阵
  • Kafka消息自定义序列化
  • golang接口用法-代码案例
  • vulhub靶场matrix-breakout-2-morpheus
  • java设计模式之工厂模式《铸剑风云录》
  • vue3:ref , reactive
  • 论华为 Pura X 折叠屏性能检测
  • S32K144外设实验(三):ADC单通道连续采样(中断)
  • AudioTrack
  • 树莓集团数字产业布局解读:战略+商业双驱动