Freezing files in a folder without a parent folder in an encrypted file

This might be a dumb question for most, but I have the following folder that I want to archive.

FolderI Folder1 Folder2 Folder3 .. .. FolderN 

Now zipping up FolderI in zipFile.zip pretty easy due to the large amount of resources on the Internet, especially here! But I want to create a zipFile.zip containing Folder1...FolderN , so when unpacking the file it will directly fill the current directory Folder1...FolderN instead of creating a FolderI that has Folder1...FolderN .

Some of the things I tried:

 $zip = new ZipArchive(); // open archive if ($zip->open('my-archive.zip', ZIPARCHIVE::CREATE) !== TRUE) { die ("Could not open archive"); } // initialize an iterator // pass it the directory to be processed $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("FolderI/")); // iterate over the directory // add each file found to the archive foreach ($iterator as $key=>$value) { $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); } // close and save archive $zip->close(); 

This was taken from compress / archive folder using php script , and I tried changing it. It does not work, but FolderI is still buttoned. But when I unzip my-archive.zip , I get FolderI and then Folder1..FolderN inside it. I also saw code from @Alix here that did the same. But, in fact, not very much.

Any help would be great. If not a code, at least point me in the right direction.

thanks

**** Does anyone know how to do this? ****

@Mods and Admins: is the solution too simple or can PHP not do this at all?

+4
source share
2 answers

The problem that arises here is that $key in the iterator is the full path (since the default flag is RecursiveDirectoryIterator::KEY_AS_PATHNAME ), which includes the relative part specified in the RecursiveDirectoryIterator constructor.

So, for example, we have the following hierarchy:

 folderI ├─ folder1 │ ├─ item.php │ └─ so.php └─ folder2 └─ item.html 

An iterator created using new RecursiveDirectoryIterator("folderI") .

When you select $key , you will get the following for item.php

 folderI/folder1/item.php 

Similarly, if we made new RecursiveDirectoryIterator("../somewhere/folderI") , then $key would look like this:

 ../somewhere/folderI/folder1/item.php 

You want to get the file path, not including the path used to create the iterator. Fortunately, RecursiveDirectoryIterator has a method whose sole purpose is to do just that.

RecursiveDirectoryIterator :: getSubPathname ()

You can use the following, which will be easier

 $newKey = $iterator->getSubPathname(); 

This will return the following for item.php , given the same folder structure as shown above:

 folder1/item.php 

The method can be used as follows:

 foreach ($iterator as $key=>$value) { $zip->addFile(realpath($key), $iterator->getSubPathname()) or die ("ERROR: Could not add file: $key"); } 

In addition, we can use $iterator->getSubPathname() here because the RecursiveIteratorIterator passes through any unknown method calls to the "internal iterator", which in this case is a recursive DirectoryIterator. Similarly, you can do $iterator->getInnerIterator()->getSubPathname() , both do the same job.

+2
source

As for directions, I can offer the following:

It looks a little more complicated for me as it is, but you can pile it up by adding a foreach loop to create these iterators like this:

 foreach (glob('./*', GLOB_ONLYDIR) as $dir_name) { // iterator // add file } 

Glob can probably do a lot more for you, perhaps eliminate those iterative objects in favor of just repeating glob output.

+1
source

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


All Articles