使用 PHP 从 ZIP 文件中删除空目录
PHP 带来了用于 ZIP 文件操作的类。它还允许使用 addEmptyDir() 创建目录并使用 deleteName() 删除条目。但删除不适用于目录(无论是否为空)。有没有办法删除 ZIP 文件中的空文件夹(首选内置 PHP 功能)?
PHP brings a class for ZIP file manipulation. It also allows the creation of directories with addEmptyDir() and the deletion of an entry with deleteName(). But the deletion does not work on directories (empty or not). Is there any way to delete empty folders in a ZIP file (prefered is buildin PHP functionality)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将 / 附加到目录名称。所以,这样的事情是有效的:
所以,testDir/ 与 testDir ....
You need to append a / to directory names. So, something like this works:
So, testDir/ versus testDir ....
您始终可以将其解压到 tmp 目录,使用 rmdir() 删除任何空目录,然后将其压缩回来。
另一件要检查的事情是权限。您确定您对尝试操作的文件具有写权限吗?
You could always just extract it to a tmp directory, delete any empty directories with rmdir() and then zip it back up.
Another thing to check is permissions. Are you sure you have write permissions on the file you are trying to manipulate?
关于目录概念的一点说明。不存在目录这样的东西。例如: foo/a.txt 和 foo/b.txt 是两个条目,但没有 foo/ 目录。但是,可以创建一个名为 foo/ 的条目来“模拟”目录。
当没有删除任何内容时返回 true 的删除方法看起来像是一个错误,我要求 Philip 在 http:// bugs.php.net 这样我就可以尽快修复它。
谢谢!
Little note about the notion of directories. There is not such thing as a directory. For example: foo/a.txt and foo/b.txt are two entries, but there is no foo/ directory. However, it is possible to create an entry called foo/ to "emulate" a directory.
The delete method returning true while nothing has been removed looks like a bug, I asked Philip to open a new bug at http://bugs.php.net so I can fix it soonish.
Thanks!