0

In this case I am trying to add all files and subdirectories to my zip file.

$zip = new ZipArchive;
$zip->open('wordpress.zip', ZipArchive::CREATE);

// Adding all files
foreach (glob(CLIENT_PATH . "/*.*") as $file) {
    $filename = substr($file, strrpos($file, '/') + 1);
    echo $filename . '<br>';
   $zip->addFile($file, 'wordpress/wp-content/themes/' . $title . '/' . $filename);
}

// Adding all subdirectories
$directories = glob(CLIENT_PATH . '/*' , GLOB_ONLYDIR);
foreach (glob(CLIENT_PATH . '/*', GLOB_ONLYDIR) as $dir) {
    $zip->addEmptyDir('wordpress/wp-content/themes/' . $title . '/' . $dir);
}

$zip->close();

Adding all files works perfectly well but adding all subdirectories won't work as expected.

This is how my unzipped file looks:

enter image description here

The subdirectory of my main directory is called assets and it also includes some more subdirectories and they also have some subdirectories. But as you see in the image above, assets includes just nothing. And also, I don't understand why it starts with home/pomcanys/ etc. instead of assets.

How can I fix this? Any help would be appreciated!

Reza
  • 513
  • 3
  • 7
  • 20
  • Why you do `GLOB_ONLYDIR`? – JustOnUnderMillions Feb 03 '17 at 15:19
  • Your code here shows only how do add files and empty folders in/from the current folder. There is no code that recursivey adds sub files/folders. Also you are doing `addEmptyDir` and `addFile` only. What its going on here? – JustOnUnderMillions Feb 03 '17 at 15:22
  • @JustOnUnderMillions you are right. I just noticed that my code sucks. Please just ignore the part "adding all subdirectories" of my code. I am just looking for something like "`addDir()`" to add directories just the way I have added files. – Reza Feb 03 '17 at 15:30
  • http://stackoverflow.com/a/1334949/4916265 – JustOnUnderMillions Feb 03 '17 at 15:39
  • Possible duplicate of [How to \[recursively\] Zip a directory in PHP?](http://stackoverflow.com/questions/1334613/how-to-recursively-zip-a-directory-in-php) – Artem Sokolov Feb 03 '17 at 16:04
  • @JustOnUnderMillions this would work but it is not adding the files to the specific sub directory that I want: wp_content/themes/theme_name – Reza Feb 03 '17 at 16:07
  • What should i do from here? What should i say know? This was just a link where it is explained how to do this. Get the needed information from there an update your code and test. – JustOnUnderMillions Feb 03 '17 at 16:10

1 Answers1

0

To automatically include files from the sub-directories use the -r parameter for recursiveness:

zip -r foo.zip *
Charles Derek
  • 35
  • 1
  • 7