ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

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

Advertisements

Creating ZIP and TAR archives on the fly with PHP

(Page 2 out of 5)

Using the temporary directory

To workaround this problem we can use the system's temporary directory, which is always writable. To get the path of the temporary directory you can use the $_ENV variable, like so:


// 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');
}
?>

As you can see in the above example there are several different variables that can contain the path of the temporary directory and each is checked. We also make sure that the temporary directory ends with a slash, and that it's writable.

Now that we've got the temporary directory, we have to create a directory where we can safely store the zip file temporarily without any interference, so let's do that:


// 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 .= '/';
?>

In the above example the tempnam() function is used to create a temporary name for our directory. After that we first make sure the name isn't used by anything else, and then proceed to create the directory.

Now that we've got a temporary directory, let's make sure our zip file gets created there, by changing the current working directory, with the chdir() function, like so:


// Change current working directory, so that the zip file gets created in the temp dir
chdir($dir);
?>

The full code now looks like this:


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

// Get temporary directory
// 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 .= '/';

// Change current working directory, so that the zip file gets created in the temp dir
chdir($dir);

// Create the zip file
$zipfile->create($list);

echo 'Zip file created in ' . $dir;

?>

There's only one problem now. Because we changed the working directory, the relative paths won't work. The obvious solution that comes to mind is to use absolute paths, but that doesn't work properly either, due to a bug in the Archive_Zip package; it doesn't work with absolute paths.

« Previous: Introduction & Creating the Archives
Next: Fixing the relative paths »



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: http://phpit.net/article/creating-zip-tar-archives-dynamically-php/  […]

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

    […] Link: http://phpit.net/article/creating-zip-tar-archives-dynamically-php/  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:

    […] http://phpit.net/article/creating-zip-tar-archives-dynamically-php/ […]

  4. PHP-Master02 Says:

    Hi,
    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
PDF
Download this article as a PDF file