How to zip folder without full path

16,966

Solution 1

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);

Solution 2

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);
?>
Share:
16,966
Admin
Author by

Admin

Updated on June 09, 2022

Comments

  • Admin
    Admin almost 2 years

    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!