zoukankan      html  css  js  c++  java
  • 图片批量压缩打包下载

        public function downloadZipImg($picAllArr, $enterprise_id)
        {
    //        $picAllArr = [
    //            'https://dss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2333857494,1212940832&fm=26&gp=0.jpg',
    //            'https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3808706043,3843571795&fm=26&gp=0.jpg'
    //        ];
    
            $tmpDir = './static/zip/images/'; // 类似于/www/public/upload/
    
            if (!file_exists($tmpDir)) {
                //创建文件夹
                mkdir($tmpDir, 0777, true);
            }
    
            //$zipName = date('His') . mt_rand(1000, 9999) . '.zip'; // 压缩包文件名
            $zipName = $enterprise_id . '.zip';
            $zipNameUrl = $tmpDir . $zipName; // 文件路径
    
            //删除之前压缩的文件
            unlink($zipNameUrl); // 删除文件
    
    
            // 生成文件
            $zip = new ipArchive();
            if ($zip->open($zipNameUrl, ipArchive::OVERWRITE) !== true) {
                //OVERWRITE 参数会覆写压缩包的文件 文件必须已经存在
                if ($zip->open($zipNameUrl, ipArchive::CREATE) !== true) {
                    // 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
                    return '下载失败,文件夹不存在';
                }
            }
            foreach ($picAllArr as $file) {
                //判断图片是否存在
                $isFile = $this->checkFileExists($file);
                if (!$isFile) {
                    continue;
                }
                //抓取图片内容
                $fileContent = file_get_contents($file);
                //添加图片
                $file = substr($file, 0, -13);
                $zip->addFromString(basename($file), $fileContent);
            }
            // 关闭
            $zip->close();
    
            //没有文件
            if (!file_exists($zipNameUrl)) {
                return '下载失败,图片不存在或无法下载';
            }
    
    //        header("Cache-Control: public");
    //        header("Content-Description: File Transfer");
    //        header('Content-disposition: attachment; filename=' . $zipName); //文件名
    //        header("Content-Type: application/zip"); //zip格式的
    //        // header("Content-Type: application/octet-stream'"); //zip格式的
    //        header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
    //        header('Content-Length: ' . filesize($zipNameUrl)); //告诉浏览器,文件大小
    //
    //        // 下面2步必须要
    //        ob_clean();
    //        flush();
            //@readfile($zipNameUrl);
            //unlink($zipNameUrl); // 删除文件
            return substr($zipNameUrl, 2);
        }
    

      

  • 相关阅读:
    Open Live Writer增加代码插件
    WinSCP列出’/’目录项出错
    Ueditor中增加迅雷下载支持
    Ueditor设置默认字体
    PDF编辑、删除、替换某页面或文字
    个人站长如何使用svn发布到服务器不遗漏文件
    PHP 测试程序运行时间 microtime函数用法
    LeetCode---Stack && Heap
    LeetCode---Binary Search
    LeetCode---Hash Table
  • 原文地址:https://www.cnblogs.com/zxqblogrecord/p/12620350.html
Copyright © 2011-2022 走看看