Logo         Community
The Company
Your Account
Contact Us
Dynamically Creating Compressed Archives With PEAR Archive_Tar
Dynamically build TAR archives using PHP

| Adding It All Up |

You can also add files to a previously-created archive with the add() method, which accepts a list of files and merely adds them to the specified archive file. If the file does not already exist, add() will create it for you.

The following example illustrates, by adding some files to the data.tar file created in an earlier example:

// include class

if (file_exists("data.tar"))
// create Archive_Tar() object
// specify filename for output file
$tar = new Archive_Tar("data.tar");

// add files to existing archive
$tar->add(array("employees.xml", "departments.xml")) or die ("Could not add files!");
die("File does not exist!");

It's important to note that the add() method will add files to the archive even if they already exist. This behaviour can lead to problems in certain situations - for example, if you ran the script above four times, the resulting archive would hold four copies of each of the files in the file list.

The ability to selectively add files to an archive comes in handy when you need to iterate over a file collection and only archive those which meet certain criteria - for example, all those files older than a specific date. Consider the following example, which illustrates how you might do this:

// include class

// get UNIX timestamp for 1 Mar 2005
$ts = mktime(0, 0, 0, 3, 1, 2005);

// set directory
$dir = "scripts/";

// create tar file
$tar = new Archive_Tar("data.tar");

// iterate over files in directory
$handle = opendir("scripts/");
while (false !== ($file = readdir($handle)))
if ($file != "." && $file != ".." && filemtime($dir . "/" . $file) < $ts)
// if file was modified before $ts
// add to archive
$tar->add($dir . "/" . $file) or die ("Could not add file!");

In this case, the filemtime() function has been used to obtain the last modification time of each file in the named directory. Files with a timestamp older than the specified date get add()-ed to the archive as they are encountered.

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback