How to zip folder without full path

允我心安 提交于 2019-12-21 12:32:29

问题


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!


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



回答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);
?>


来源:https://stackoverflow.com/questions/11317928/how-to-zip-folder-without-full-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!