SunQuest
 
       PHP
  Home arrow PHP arrow Page 4 - TAR File Management With PHP Archive_T...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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: 4 stars4 stars4 stars4 stars4 stars / 30
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway