PHP Zip Archive 偶尔创建多个文件

发布于 2024-10-11 15:43:56 字数 948 浏览 5 评论 0原文

我有一个 PHP 脚本(在 Apache 2.2 上运行 PHP 5.2),它创建文件的 zip 存档供用户下载。一切似乎都运转良好;我遇到的唯一问题是,在 ZIP 存档成功完成之前,会定期创建多个文件。示例:

archive-name.zip

archive-name.zip.a09600

archive-name.zip.b09600

archive-name.zip.c09600

这并不是每次都会发生;仅定期 - 文件仍然会创建,但有时这些额外的“文件”在完成后会留在服务器上。创建 ZIP 存档的代码如下:

        $zip_archive = new ZipArchive();
        $zip_archive->open($archiveFullName,ZIPARCHIVE::CREATE);

        if(!file_exists($archiveFullName)) {
            foreach ($completed_file_arr as $zip_file) {
                $isFiller = substr($zip_file,-8) == "_err.txt";
                if(!$isFiller) {
                    $zip_archive->addFile($zip_file,$localname);
                } else $zip_archive->addFromString ($zip_file, "The requested source file could not be found.");
            }
        }

       while(!$zip_archive->close()) sleep(1); //ensure that the ZIP file finishes closing

I have a PHP script (running PHP 5.2 on Apache 2.2) that creates a zip archive of files for a user to download. Everything seems to work just fine; the only issue I have is that periodically multiple files will be created before the ZIP archive is successfully finished. Example:

archive-name.zip

archive-name.zip.a09600

archive-name.zip.b09600

archive-name.zip.c09600

This does not happen every single time; only periodically - the file is still created but sometimes these extra 'files' are left on the server after it finishes. The code that creates the ZIP archive is as follows:

        $zip_archive = new ZipArchive();
        $zip_archive->open($archiveFullName,ZIPARCHIVE::CREATE);

        if(!file_exists($archiveFullName)) {
            foreach ($completed_file_arr as $zip_file) {
                $isFiller = substr($zip_file,-8) == "_err.txt";
                if(!$isFiller) {
                    $zip_archive->addFile($zip_file,$localname);
                } else $zip_archive->addFromString ($zip_file, "The requested source file could not be found.");
            }
        }

       while(!$zip_archive->close()) sleep(1); //ensure that the ZIP file finishes closing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘曦 2024-10-18 15:43:56

事实证明这实际上是一个与会话相关的问题;会话数据不会在第一次传递时保存,因为调用了代码中更高层的函数,该函数多次调用 session_write_close() (解决了 exec() 的已知错误)。问题在于会话再次启动后保存/检索数据。

This turned out to actually be a session related problem; the session data isn't being saved on the first pass because of calls to a function higher up in the code that calls session_write_close() several times (workaround to a known bug with exec() ). The problem was in preserving/retrieving the data after the session started back up again after that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文