Creating ZIP and TAR archives on the fly with PHP
(Page 4 out of 5)Creating a re-usable function
To make it even easier to create zip files, let's change our script into a re-usable function:
function create_archive ($name, $list) {
// Get temporary directory
if (!empty($_ENV['TMP'])) {
$tempdir = $_ENV['TMP'];
} elseif (!empty($_ENV['TMPDIR'])) {
$tempdir = $_ENV['TMPDIR'];
} elseif (!empty($_ENV['TEMP'])) {
$tempdir = $_ENV['TEMP'];
} else {
$tempdir = dirname(tempnam('', 'na'));
}
if (empty($tempdir)) { die ('No temporary directory'); }
// Make sure trailing slash is there
$tempdir = rtrim($tempdir, '/');
$tempdir .= '/';
// Make sure temporary directory is writable
if (is_writable($tempdir) == false) {
die ('Temporary directory isn\'t writable');
}
// Create temp name for our own directory
$dir = tempnam($tempdir, 'temp');
// Make sure another file or directory doesn't already exist with this name
@unlink($dir);
@rmdir($dir);
// Create directory
mkdir($dir);
$dir .= '/';
// Copy files & directories so relative paths still work
foreach ($list as $item) {
if (is_file($item)) {
// Copy file
copy ($item, $dir . $item);
} elseif (is_dir ($item)) {
// Copy directory
move_directory ($item, $dir . $item, true);
} else {
// Invalid file, just ignore
continue;
}
}
// Change current working directory, so that the zip file gets created in the temp dir
chdir($dir);
// Create instance of Archive_Zip class, and pass the name of our zipfile
$zipfile = New Archive_Zip($name . '.zip');
// Create the zip file
$zipfile->create($list);
// Return file data
$data = implode('', file($name . '.zip'));
return $data;
}
?>
The above function will create the zip file, and return the data, which can then be used to stream to the user (which I will show you later in this article). It's used like this:
// Create a list of files and directories
$list = array('example.txt', 'test');
// Create the archive
$data = create_archive ('myzipfile', $list);
echo 'Zip file created, size is ' . strlen($data) . ' bytes';
?>
Because we've now got a nice compact and re-usable function, it's easy to create TAR archives as well. The Archive_Tar package works almost the same, so we have to change very little.
First include the Achive_Tar package:
include ('pear/Tar.php');
?>
And then change the create_archive function slightly:
// Get temporary directory
if (!empty($_ENV['TMP'])) {
$tempdir = $_ENV['TMP'];
} elseif (!empty($_ENV['TMPDIR'])) {
$tempdir = $_ENV['TMPDIR'];
} elseif (!empty($_ENV['TEMP'])) {
$tempdir = $_ENV['TEMP'];
} else {
$tempdir = dirname(tempnam('', 'na'));
}
if (empty($tempdir)) { die ('No temporary directory'); }
// Make sure temporary directory is writable
if (is_writable($tempdir) == false) {
die ('Temporary directory isn\'t writable');
}
// Create temp name for our own directory
$dir = tempnam($tempdir, 'temp');
// Make sure another file or directory doesn't already exist with this name
@unlink($dir);
@rmdir($dir);
// Create directory
mkdir($dir);
$dir .= '/';
// Copy files & directories so relative paths still work
foreach ($list as $item) {
if (is_file($item)) {
// Copy file
copy ($item, $dir . $item);
} elseif (is_dir ($item)) {
// Copy directory
move_directory ($item, $dir . $item, true);
} else {
// Invalid file, just ignore
continue;
}
}
// Change current working directory, so that the zip file gets created in the temp dir
chdir($dir);
// Create zip or tar.gz file
if ($type == 'zip') {
$name .= '.zip';
$zipfile = New Archive_Zip($name);
$zipfile->create($list);
} else {
$name .= '.tar.gz';
$tarfile = New Archive_Tar($name, 'gz');
$tarfile->create($list);
}
// Get file data
$data = implode('', file($name));
return $data;
}
As you can see we've added a third parameter to the function called $type to specify the type of archive. At the end of the function we've also made a few changes, making it possible to create a zip file or tar.gz file.
And that's all there is to it. It's now possible to dynamically create ZIP and TAR.GZ archives. Let's look at the final part: sending the archive to the visitor.
May 18th, 2006 at 12:34 am
[…] Link: http://phpit.net/article/creating-zip-tar-archives-dynamically-php/ […]
May 18th, 2006 at 12:36 am
[…] Link: http://phpit.net/article/creating-zip-tar-archives-dynamically-php/ Posted by Administrator on May 18th, 2006 Filed in tutorials, php, links, php […]
May 20th, 2006 at 6:03 am
[…] http://phpit.net/article/creating-zip-tar-archives-dynamically-php/ […]
June 12th, 2006 at 9:00 am
Hi,
This was very interesting and gave me lots of thoughs to create new things,,
thanks alot
July 5th, 2006 at 3:13 am
Hi, Thanks a lot for the detailed tutorial… I have one problem, although: When I executed your script, it simply downloads the actual “example.txt” file (zipped up) instead of the array of files. I’m very new to php so apologies for my ignorance in advance but is there something I’m missing?
July 23rd, 2006 at 5:23 pm
Hi, this is Andrew again and I am still having the same problem almost 3 weeks later. If I implement the script exactly “As is”, it runs perfectly fine and I download the zip file but it only has 1 file in it… the “example.txt” file. Any ideas? I can’t find any other zip file tutorials…
August 10th, 2006 at 2:00 pm
Just tried it and it worked fine, very good article. First it didn’t put any file in the zipfile altough i filed my filelist array correctly, but later I saw it was due to a mistake in filepaths (I forgot a directory in the path).
Andrew ->Did you check that your filepaths are OK?