Remove empty directory from zip file using PHP

PHP brings a class for handling ZIP files . It also allows you to create directories with addEmptyDir () and delete a record using deleteName (). But deletion does not work in directories (empty or not). Is there a way to delete empty folders in a ZIP file (preferably the built-in PHP function)?

+4
source share
3 answers

You need to add the names / to the directory. So something like this works:

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

So testDir / versus testDir ....

+1
source

You can always just extract it into the tmp directory, delete all empty directories with rmdir (), and then zip it back.

Another thing to check is permissions. Do you have write permissions to the file you are trying to manipulate?

0
source

A short note on the concept of directories. There is no such thing as a directory. For example: foo / a.txt and foo / b.txt are two entries, but there is no foo / directory. However, you can create an entry called foo / to "emulate" the directory.

The delete method, which returns true when nothing was deleted, looks like an error, I asked Philip to open a new error at http://bugs.php.net so I can fix it soon.

Thanks!

0
source

Source: https://habr.com/ru/post/1309984/


All Articles