开发者

Remove an empty directory from a ZIP file with 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 (pr开发者_高级运维efered is buildin PHP functionality)?


You need to append a / to directory names. So, something like this works:

<?php
    $zip = new ZipArchive;
    if ($zip->open('test.zip') === TRUE) {
        $zip->deleteName('testDir/');
        $zip->close();
    }
?>

So, testDir/ versus testDir ....


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?


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜