将文件添加到存档后删除文件会阻止创建存档 [英] Deleting files after adding them to an archive prevents the archive's creation

查看:32
本文介绍了将文件添加到存档后删除文件会阻止创建存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    if ($zip->open($zipFile, ZipArchive::CREATE) !== TRUE) {
        (...)
    } else {
        $zip->addFile($tempDir.$fileName.".xls", $fileName.".xls");
        // The array contains the directory structure of the files to add
        foreach ($list_attachments as $dir_name => $attachment_files) {
            if (!empty($attachment_files)) {
                $zip->addEmptyDir($dir_name);
                foreach ($attachment_files as $attachment) {
                    $zip->addFile($tempDir.$dir_name."/".$attachment, $dir_name."/".$attachment));
                    unlink($tempDir.$dir_name."/".$attachment);
                }
                rmdir($tempDir.$dir_name."/");
            }
        }
        $zip->close();
    }

请不要介意变量名称中潜在的拼写错误,我重新编写了它们和英文注释,以使它们更具可读性.如果我按原样运行代码,它将删除文件和目录,但不会创建存档.我检查了返回值,addEmptyDiraddFileunlinkrmdir 一切正常.但是,删除文件似乎会阻止存档正常关闭,因此不会创建文件.

Please don't mind potential typos in the variable names, I rewrote them and the comment in English to make them more readable. If I run the code as is, it will delete the files and the directories but won't create the archive. I ran checks on return values and addEmptyDir, addFile, unlink and rmdir all work fine. However, it seems that removing the files prevents the archive from closing properly, and thus the file isn't created.

我通过移动 unlinkrmdir 调用之后 $zip->close() 来绕过它>,因此只有在创建存档后才会删除文件.然而,这迫使我有两次循环,从我收集到的文档和与 zip 相关的问题来看,这里不应该像我那样取消链接有任何问题.有谁知道这可能发生的原因是什么?

I circumvented it by moving the unlink and rmdir calls after the $zip->close(), so the files are only deleted after the archive is created. However, is forces me to have twice the loops, and from what I've gathered looking at the documentation and zip-related questions here there shouldn't be any issue with unlinking like I did. Does anyone know for which reason this could happen?

推荐答案

zip 将在您调用 $zip->close() 后最终写入文件代码>.直到此时一切都发生在内存中,没有完成压缩".这就是为什么只有在成功调用 $zip->close() 后才能删除解压文件.

The zip will be finally written to file AFTER you've called $zip->close(). Until this point everything happens in memory, no 'zipping' is done. That's why you can delete the unzipped files only after you've called $zip->close() successfully.

文档甚至说:

当一个文件被设置为添加到存档时,PHP 将尝试锁定该文件,并且只有在 ZIP 操作完成后才会释放它.简而言之,这意味着您可以在存档关闭后先删除添加的文件.

When a file is set to be added to the archive, PHP will attempt to lock the file and it is only released once the ZIP operation is done. In short, it means you can first delete an added file after the archive is closed.

然而,无论如何,锁不会阻止您删除文件,它们只是提示",最大的问题是文件需要在close()上进行处理.

However, the locks will not prevent you from deleting the files anyway, they are just "hints", the big problem is that the files need to be there for processing on close().

所以内循环应该是这样的:

So the inner loop should look like this:

foreach ($attachment_files as $attachment) {
    $zip->addFile($tempDir.$dir_name."/".$attachment, $dir_name."/".$attachment));
    $to_be_unlinked []= $tempDir.$dir_name."/".$attachment;
}

稍后,取消链接文件:

...
foreach($to_be_unlinked as $file) {
    unlink($file);
}

这篇关于将文件添加到存档后删除文件会阻止创建存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆