8

firstly I don't which is the correct stackExchange site to post this question if the question is for other stack-site please remove my question.

Now let talk about the question: This situation is I have a file which is located in: /home/user/public_html/folder-one/folder-two/folder-three/file.php and the file must create archive of folder /folder-one with all files and sub-folders of /folder-one. I create the archive with system function (exec(), shell_exec() or system()) and it works perfect. My code is:

<?php
$output = 'zip -rq my-zip.zip /home/user/public_html/folder-one -x missthis/\*';
shell_exec($output);
?>

But when I download and open the archive the archive include the sub-folders as /home ; /user ; /public_html but this folders are totally unneeded and I wanna know how to create zip without them.

When I try with something like this $output = 'zip -rq my-zip.zip ../../../folder-one -x missthis/\*'; but then when I open the archive (on Windows 7 based OS) the name of folder-one is ../folder-one

Postscript: It will be better if somebody gives me correct $output the make zips on windows based hosting plans.

Best regards, George!

2 Answers2

14

By default, zip will store the full path relative to the current directory. So you have to cd into public_html before running zip:

$output = 'cd /home/user/public_html; zip -rq my-zip.zip folder-one -x missthis/\*';
shell_exec($output);
vkorchagin
  • 636
  • 4
  • 7
6

There is a better way, without doing a cd . As stated here there is an option to ignore full paths. This is -j or --junk-paths. So you could do:

<?php
$output = 'zip -jrq my-zip.zip /home/user/public_html/folder-one -x missthis/\*';
shell_exec($output);
?>
Community
  • 1
  • 1
pgmank
  • 5,303
  • 5
  • 36
  • 52
  • 2
    -j will leave `/folder_one` out, and it sounds like OP needs to have that directory and its contents in the zip archive, not just the contents of the folder – sixty4bit Nov 21 '17 at 21:33