PHP Classes

File: example1.php

Recommend this page to a friend!
  Classes of Colin McKinnon   pfpFileTree   example1.php   Download  
File: example1.php
Role: Example script
Content type: text/plain
Description: example
Class: pfpFileTree
Manipulates files in directories recursively
Author: By
Last change:
Date: 13 years ago
Size: 1,116 bytes
 

Contents

Class file image Download
<?php

/**
 * this example would list all the image files
 * found in /home/user/public_html
 * which do not also exist in /var/www/html
 * and copy over the missing files
 */

// ----------- initialise ------------
require_once('pfpFileTree.inc.php');

$src='/home/user/public_html';
$dest='/var/www/html'; // NB class wil sort out trailing dir seperators

$t=new pfpFileTree($src);

$t->ignoreNoDescend=true; // ignore some errors
$t->caseSensitive=false; // used for globbing only

// --------- grab images -----------
$t->readTree(array('name'=>'*.jpg'));
$t->readTree(array('name'=>'*.png'));
$t->readTree(array('name'=>'*.gif'));
// note it would have been much more efficient to...
// $t->readTree(array('name'=>'{*jpg,*.png,*.gif}');

// $t->data is now populated with a list of all image files

$t->ls();

// --------- compare with dest ---------
$t->compareTree($dest);
$files=$t->filter(array('cmp'=>2)); // just find the missing files

if ($t->writeTo($dest)===false) {
    print
"Copy would have failed for: ";
   
$nocopy=$t->subset(array('can_w'=>false));
   
$nocopy->ls();
} else {
    print
"files copied";
}