ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.


Creating ZIP and TAR archives on the fly with PHP

(Page 1 out of 5)


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.


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('');

// Create a list of files and directories
$list = array('example.txt');

// Create the zip file

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.

Next: Using the Temporary Directory »

7 Responses to “Creating ZIP and TAR archives on the fly with PHP”

  1. Il blog sul php » Creare un archivio ZIP o TAR al volo con php Says:

    […] Link:  […]

  2. Creare un archivio ZIP o TAR al volo con php - Says:

    […] Link:  Posted by Administrator on May 18th, 2006 Filed in tutorials, php, links, php […]

  3. Giao’s Journal » Blog Archive : Creating ZIP and TAR archives on the fly with PHP Says:

    […] […]

  4. PHP-Master02 Says:

    This was very interesting and gave me lots of thoughs to create new things,,
    thanks alot

  5. Andrew Says:

    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?

  6. Andrew Says:

    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…

  7. Fab Says:

    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?

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Creating the Archives
  2. Using the Temporary Directory
  3. Fixing the relative paths
  4. Creating a re-usable function
  5. Streaming files & Conclusion
Bookmark Article
Download Article
Download this article as a PDF file