This question already has an answer here:

I'm running:

zip -r /var/www/html/example.com/temp/test.zip /var/www/html/example.com/files_to_zip/

which works but gives me the full directory structure of /var. I saw the -j flag which removes these directories, but it also removes the directories in files_to_zip. Is there a way to recursively build my zip from the files_to_zip directory?

I'm running Amazon Linux 2 and my zip is using Zip 3.0.

So the previously described behaviors are demonstrated with:

exec('zip -r ' . $temp_dir . 'test.zip ' . $zip_dir . '/');
exec('zip -rj ' . $temp_dir . 'test.zip ' . $zip_dir . '/');

I've modified my code per the provide dup and answer to:

exec('(cd ' . $zip_dir . '; zip -r ' . $temp_dir . 'test.zip ' . $zip_dir . '/)'); 
exec('cd ' . $zip_dir . '; zip -r ' . $temp_dir . 'test.zip ' . $zip_dir . '/');

per https://stackoverflow.com/a/1679060/3783243 I also tried:

exec('(cd ' . $zip_dir . ' && zip -r ' . $temp_dir . 'test.zip ' . $zip_dir . '/)');
exec('cd ' . $zip_dir . ' && zip -r ' . $temp_dir . 'test.zip ' . $zip_dir . '/');

I've also tried a PHP solution to this:

exec('pwd', $result);

which returned the results of $zip_dir.

The behavior is the same, my zip has the full path from /var. After every code execution the zip size increases by 1 byte though.

marked as duplicate by Stephen Kitt linux May 16 at 13:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You're still (probably) telling zip to use the full path of zip_dir -- when the answers here say to cd to the parent (as you've almost done in the later examples) and then to use the relative directory name – Jeff Schaller May 16 at 14:20

Consider something like:

(cd /var/www/html/example.com; zip -r /var/www/html/example.com/temp/test.zip files_to_zip)
  • I've tried this but the behavior is the same. I also tried it with the chdir(); in PHP per the dup but that also behaved the same. I verified with exec('pwd', $return); that I was moved to the correct directory as well. – user3783243 May 16 at 14:03
  • I'd recommend you show the exact steps you took and their exact results -- in an edit to the question. My answer is the same as the one Stephen linked to, and should work, so something's missing. – Jeff Schaller May 16 at 14:04
  • Okay, I've updated the question. Please let me know if there's more details I should provide. – user3783243 May 16 at 14:18
  • This behaves as expected in the linux system. – user3783243 May 16 at 14:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.