PHP
  Home arrow PHP arrow Page 4 - TAR File Management With PHP Archive_Tar
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

TAR File Management With PHP Archive_Tar
By: The Disenchanted Developer, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 35
    2003-07-17


    Table of Contents:
  • TAR File Management With PHP Archive_Tar
  • Back To Basics
  • Zip Zap Zoom
  • Adding It All Up
  • Building An Index
  • In And Out
  • X-Ray Vision
  • ... And Packing Up

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    TAR File Management With PHP Archive_Tar - Adding It All Up
    ( Page 4 of 8 )

    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 the previous example:


    <?php

    // include class
    require("Tar.php");

    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!"); } else {
    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:


    <?php

    // include class
    require("Tar.php");

    // get UNIX timestamp for 1 Jun 2003
    $ts = mktime(0, 0, 0, 6, 1, 2003);

    // 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!");
    }
    }
    closedir($handle);

    ?>

    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.



     
     
    >>> More PHP Articles          >>> More By The Disenchanted Developer, (c) Melonfire
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT