Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Dynamically Creating Compressed Archives With PEAR Archive_Tar
Dynamically build TAR archives using PHP

| Back To Basics |

The Archive_Tar class is brought to you by PEAR, the PHP Extension and Application Repository. In case you didn't know, PEAR is an online repository of free PHP software, including classes and modules for everything from data archiving to XML parsing. When you install PHP, a whole bunch of PEAR modules get installed as well; the Archive_Tar class is one of them.

In case your PHP distribution didn't include Archive_Tar, or if you chose not to install the PEAR component, you can get yourself a copy from the official PEAR Web site, at http://pear.php.net/package/Archive_Tar - simply unzip the distribution archive into your PEAR directory and you're ready to roll!

Let's begin with something simple - packing a bunch of files into a single TAR archive with Archive_Tar() object methods (this tutorial uses Archive_Tar 1.3.1).


<?php
// include class
require("Archive/Tar.php");

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

// set up file list
$files = array("package.dtd", "package.xml", "filter.xsl", "../../includes/php/adodb.php");

// build archive
$tar->create($files) or die("Could not create archive!");
?>


The first step is to include the required class file in your script, and instantiate an Archive_Tar() object.


<?php
// include class
require("Archive/Tar.php");

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


Note that the object constructor requires, as argument, the name of the TAR file to be created. You can include a path here as well if you would like the file to be created in a location other than the current directory.

Third, you need to tell the newly-created object which files to include in the archive. This information is provided to the object's create() method as an array of file names.


<?php
// set up file list
$files = array("package.dtd", "package.xml", "filter.xsl", "../../includes/php/adodb.php");

// build archive
$tar->create($files) or die("Could not create archive!");
?>


Obviously, you should replace the file list in the example above with information that reflects your own system. If Archive_Tar cannot locate any of the files specified in the file list, it will simply skip over it to the next one.

Note that the create() method will replace a previously-existing file with the same name. If you don't want this to happen, you should wrap your call to create() in a file_exists() test.

Once you run this script through your browser, you'll see that a file named data.tar appears in the same directory as the script.


$ ls -l data.tar
-rwxrw-rw-    1 nobody   nobody      72704 Jul  8 14:31 data.tar


This is the TAR file created by the lines of code above.

If you examine this file with the tar command, you'll see that it contains all the files specified in your PHP script.


$ tar -tvf data.tar
-rw-rw-rw- 0/0            2876 2002-04-09 21:04:08 package.dtd
-rw-rw-rw- 0/0            2891 2002-11-18 01:22:20 package.xml
-rw-rw-rw- 0/0             707 2003-02-19 19:18:14 filter.xsl
-rw-rw-rw- 0/0           62790 2002-06-12 17:17:20 includes/php/adodb.php



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
 
Search...
 
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
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2017 Melonfire. All rights reserved
Terms and Conditions | Feedback