I want to add to the archive a folder in which there are subfolders and files in them.

$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); 

I can not use this, because I do not need all the files.

There is an array with some information about the files:

 $paths = [ [ 'name' => 'file', 'ext' => 'txt', 'path' => '/folder/' ], [ 'name' => 'subFolder', 'ext' => 'folder', 'path' => '/folder/' ], [ 'name' => 'fileInSubFolder', 'ext' => 'txt', 'path' => '/folder/subFolder/' ] ]; 

In the loop I do a check:

 if( $folder[$i]['ext'] == 'folder' ) $zip->addEmptyDir($fileName); else $zip->addFromString( $fileName.$fileExt, file_get_contents($fileFullPath.$fileExt)); 

If the "folder", then create an empty folder in the archive, otherwise I add the file to the archive. Only if the file is in a subfolder, is it added to the root of the archive, and how to add it to the subfolder? Or how to change the current directory to add?

  • And what a mistake? What is not obtained? And before the brackets everywhere. - Naumov
  • There are no errors. I have a mainfolder structure -> folder -> subfolder. In the folder there is a file file.txt. In the subfolder there is a file fileInSubFolder.txt. I archive the mainfolder, but in the archive everything will look like this: file.txt fileInSubFolder.txt folder subFolder Those. everything will be on the same level. Fundamentally. What are the brackets to arrange? - Seraphim

1 answer 1

Problem solved. It was necessary in the addFromString function to addFromString first parameter to the path to the file + name + extension, and not just the name. Immediately did not guess. I thought everything was more complicated.