Creating ZIP and TAR archives on the fly with PHP
(Page 1 out of 5)Abstract
In this tutorial you will learn how to create ZIP and TAR archives dynamically using PHP and the PEAR::Archive_Zip and PEAR::Archive_Tar packages.
Introduction
The easiest way to offer files for downloading on your website is to simply create the necessary archives (using WinZip or another program) on your own computer, and uploading the archives. But in some cases this isn't enough, for instance when you want to generate a unique archive for each person who downloads the archive. This means you'll have to create ZIP and TAR archives on the fly with PHP.
In this tutorial I will show you exactly how to do that. Thankfully there are two excellent libraries in the PHP Extension and Application Repository (PEAR) which makes it a lot easier since all the hard stuff has been written for us already.
You will also learn how to stream these dynamically created archives using the right headers so that the browser will know it's an archive, and not a normal PHP page.
Let's start with the most important: creating the archives.
Creating the archives
To create the archives we'll be using the PEAR::Archive_Zip package and the PEAR::Archive_Tar package. Since they're PEAR packages, you'll also have to download the main PEAR.php file. Either you can download and install the full PEAR library, or you can download the PEAR.php file by clicking here. Since you don't need anything except the PEAR.php file I recommend downloading it from the link I just gave you, instead of installing the full PEAR library.
Let's start of with the PEAR::Archive_Zip package. It's surprisingly easy to use, and it only takes three lines to create a new zip file:
include ('pear/archive_zip.php');
// Create instance of Archive_Zip class, and pass the name of our zipfile
$zipfile = New Archive_Zip('myzipfile.zip');
// Create a list of files and directories
$list = array('example.txt');
// Create the zip file
$zipfile->create($list);
echo 'Zip file created';
?>
Let's go through this example. First we include the PEAR::Archive_Zip package, after which we create an instance of the Archive_Zip class and pass the name of the zipfile we are going to create as the first parameter.
Then we build a list of all the files and directories that should be included in the zipfile. In the example we include only one file called example.txt which is located in the same directory as the script. This list of files is passed to the create() method of the object, and the actual zip file is created, and will be located in the same path as your script.
All seems very simple, but there are a few catches. For one, if the path isn't writable it won't be possible to create the zip file, and in many cases, the script path isn't writable, so we'll have to think of a solution.
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?