I just wanted to post this code snippet up here in case anyone else is stuck Googling for many hours without success how to do this...

I have a PHP script (actually, a Drupal module I'm working on) set up to grab a folder, archive it using tar with the -z (gzip/compress) option, and post a link to the archive. However, since I was using PHP, I couldn't do what I normally do when tar-ing a folder, that is, I couldn't change directory (cd) first, then create the archive.

The problem this creates (for me) is that, upon expanding the PHP-created tar.gz archive, the system folder/directory heirarchy is expanded as well... so my files are buried in a folder 5-10 levels deep. Not ideal for zipping up a bunch of photos or something.

So, to overcome this problem, you can use tar's -C option, which allows you to change directory before tar-ing the files:

tar -C /directory/of/files-to-archive -zcvf /path/to/final-archive.tar.gz .

This simply creates an archive that expands to a folder with the archive's name, and has all the files contained directly inside!

My final command in the PHP script used the exec() function, which allows command-line commands from a PHP script.