Class: Dir extends Directory
- Package: saf.File
- Author: John Luxford <lux@simian.ca>
- Copyright: Copyright (C) 2001-2003, Simian Systems Inc.
- License: http://www.sitellite.org/index/license Simian Open Software License
- Version: 1.8, 2002-08-23, $Id: Directory.php,v 1.5 2008/02/22 09:53:34 lux Exp $
- Access: public
Dir is basically a wrapper class around PHP's internal Directory
class which provides a little more functionality. It really only defines an
additional method read_all(), which allows you to retrive directory lists
sorted by a number of properties, including alphabetically (by name), by
date (most recently modified first), and by size (largest on top).
New in 1.2:
- find () method will search through directories (recusion is optional) for
files based on patterns, which are parsed by the matches () method.
New in 1.4:
- find_in_files() method will search through directories (recursion is
optional) for files whose contents contain a specified string or pattern,
which relies on the File class for the content evaluation.
New in 1.6:
- build() method will build a directory structure for you recursively if any
parts of the path do not exist.
- rmdir_recursive() method will remove a directory and all of its contents.
New in 1.8:
- Added an open() method, so that you can reuse the same Directory object.
Note: All the other methods are identical to PHP's internal Directory class.
Usage Example
<?php
$dir = new Dir ("/path/to/dir/you/want");
// read the directory in the traditional method, where sorting must be
// done yourself...
while ($file = $dir->read ()) {
// do something with $file
}
$dir->rewind ();
// now let's do it with the new read_all() method, and sort by size
$list = $dir->read_all ("size");
foreach ($list as $file) {
// do something with $file
}
$dir->close ();
?>
Properties
$handle = ""
- Access: public
Contains the handle of the currently open directory.
$path = ""
- Access: public
Contains the path provided to the currently open directory.
$_ls = array ()
- Access: private
Contains the list of files retrived from the previous read_all()
method call.
Methods
Dir ($dir_string = "")
- Access: public
Constructor method. Opens the specified directory if one is provided.
open ($dir_string)
- Access: public
Opens the specified directory.
readAll ($sorting_method = "alphabetical")
- Access: public
- Return: array
Read and sort the contents of the directory and return them as an array.
$sorting_method can be either 'alphabetical' (the default), 'date', or 'size'.
Note: read_all() does not remove any entries from this list, such as . and ..
read_all ($sorting_method = "alphabetical")
Deprecated. Use readAll() instead.
Exists for backward-compatibility.
read ()
- Access: public
- Return: string
Read the contents of the directory and return them one at a time.
rewind ()
- Access: public
Rewinds the internal cursor for the purpose of re-reading the directory contents.
close ()
- Access: public
Closes the current directory.
_sort ($method)
- Access: private
Sorts the contents of the private array $_ls, by the method requested.
$method can be 'alphabetical', 'size', or 'date'.
matches ($pattern = '*', $file)
- Access: public
- Return: boolean
Compares a file name to a pattern string, which may contain
simple * wildcards.
find ($pattern = '*', $basedir, $recursive = 0)
- Access: public
- Return: array
Searches a directory or directories (recursively) for a file
that matches a given pattern.
findInFiles ($string, $basedir, $recursive = 0, $regex = 0)
- Access: public
- Return: array
Searches a directory or directories (recursively) for a file
that contains a given string or pattern. Note: this method requires
the saf.File class.
find_in_files ($string, $basedir, $recursive = 0, $regex = 0)
Deprecated. Use findInFiles() instead.
Exists for backward-compatibility.
build ($path, $mode = 0700)
- Access: public
- Return: boolean
Takes a directory path and builds each level if they don't exist.
For example: calling Dir::build('sitellite/mod/newmodule/pix') while in
the base directory of your site would change directories to sitellite/mod
then call mkdir('newmodule'), chdir('newmodule'), and finally mkdir('pix'),
before decending back to the directory you started in. Note: This method
does not affect the current working directory. Also note that the $mode
must be specified as a 4-digit octal value. See php.net/mkdir for more
information on $mode values.
rmdirRecursive ($path = '')
- Access: public
- Return: boolean
Removes a directory and all of its contents.
rmdir_recursive ($path = '')
Deprecated. Use rmdirRecursive() instead.
Exists for backward-compatibility.
fetch ($path, $skipDots = false)
- Access: public
- Return: array files
Fetches all the files in a directory in a single command,
ie. $files = Dir::fetch ('foo/bar');
getStruct ($path)
- Access: public
- Return: array files
Returns the "structure" of the filesystem below the specified path.
This is a list of sub-directories, recursively.
