In PHP 5.3+ you can use the PharData class to create the tar:
// list of file paths to add
$files = glob('/docs/*.pdf');
$tar = new PharData(__DIR__ . '/myfiles.tar');
foreach ($files as $f) {
$tar->addFile($f, basename($f));
}
If the tar isn't very large (or you have a lot of RAM), you can then compress with:
$tar->compress(Phar::GZ);
For large files, which could throw a memory limit error with the above method, one alternative is:
// you might want to adjust the time limit
set_time_limit(0);
$source = fopen($pathToTar, 'rb');
$target = __DIR__ . '/myfiles.tar.gz';
$destination = fopen('compress.zlib://' . $target, 'wb');
stream_copy_to_stream($source, $destination);
Reference:
http://www.binarytides.com/how-to-create-tar-archives-in-php/