php 把文件加入到现有的zip压缩包中

// 指定要添加的文件和已有的压缩包名称
        $fileToAdd = public_path().'pdffile/file1.txt'; // 要添加的文件
        $zipFileName = public_path().'pdfzip/archive.zip'; // 已有的压缩包名称

        // 创建一个新的ZIP档案对象
        $zip = new ZipArchive();
        if ($zip->open($zipFileName, ZipArchive::CREATE) === TRUE) {
            // 检查要添加的文件是否存在
            if (file_exists($fileToAdd)) {
                $zip->addFile($fileToAdd, basename($fileToAdd)); // 添加文件到压缩包
                echo "文件 $fileToAdd 已成功添加到压缩包 $zipFileName \n";
            } else {
                echo "文件 $fileToAdd 不存在。\n";
            }
            // 关闭ZIP档案
            $zip->close();
        } else {
            echo "无法打开压缩包 $zipFileName \n";
        }

 

posted @ 2024-08-28 18:26  西瓜霜  阅读(0)  评论(0编辑  收藏  举报