Click to See Complete Forum and Search --> : Directory Functions


stfuji
11-22-2004, 01:43 PM
Anyone have faster smarter?


<?php

############################################################
############################################################
## Functions: Directory
## Functions designed to work with directories
##
## November 17, 2004
## Geoffrey DeFilippi
##
## Listing:
##
## is_valid_directory( $path, $error (returned will be erased at entry point) ){
## Checks a path to make sure it is valid returns true or false
## and an error message if false
##
## load_directory_dfs( $path (string), $filter (regex expression) )
## Load a directory of files with include_once directive if their
## name match the regex expressions passed in as a string
## or load everything (Depth First)
##
## load_directory_bfs( $path (string), $filter (regex expression) )
## Load a directory of files with include_once directive if their
## name match the regex expressions passed in as a string
## or load everything (Breadth First)
##
############################################################
############################################################

////////////////////////////////////////////////////////////
// INSTALLATION VARIABLE
define( 'INSTALLED_DIRECTORY_FUNCTIONS', 1 );

////////////////////////////////////////////////////////////
// Start Code
function is_valid_directory( $path = "./", &$error) {

$error = "";
if( ! file_exists( $path ) ) {
$error = "Error: Attempting to load from no existant directory. A valid directory is required";
return false;
} elseif ( ! is_dir( $path ) ) {
$error = "Error: Attempting to load file. A directory is required";
return false;
} elseif ( ! is_readable( $path ) ) {
$error = "Error: Directory is not readable. A readable directory is required";
return false;
}
return true;

}

function load_directory_dfs( $path = "./", $filter = '/^.*$/' ) {

$error = "";
if( ! ( is_valid_directory( $path, $error ) ) ) {
die( $error );
}

if( ! $directory = @opendir( $path ) ) {
die( "Unable to open dir: error message here" );
}

while( $file = @readdir( $directory ) ) {

if( ( preg_match( $filter, $file ) ) && ( ! is_dir( $path.$file.'/' ) ) ) {
include_once( $path . $file );
}
elseif ( ( is_dir( $path.$file.'/' ) ) && ( ! preg_match( '/^\.|\.\.$/', $file ) ) ) {
load_directory_dfs( $path.$file.'/', $filter );
}
}

@closedir( $directory );
}

function load_directory_bfs( $path = "./", $filter = '/^.*$/' ) {

$error = "";
if( ! ( is_valid_directory( $path, $error ) ) ) {
die( $error );
}

if( ! $directory = @opendir( $path ) ) {
die( "Unable to open dir: error message here" );
}

$queue = array();

while( $file = @readdir( $directory ) ) {

if( ( preg_match( $filter, $file ) ) && ( ! is_dir( $path.$file.'/' ) ) ) {
include_once( $path . $file );
}
elseif ( ( is_dir( $path.$file.'/' ) ) && ( ! preg_match( '/^\.|\.\.$/', $file ) ) ) {
$queue[] = $path.$file.'/';
}
}

@closedir( $directory );

while( count( $queue ) > 0 ) {

load_directory_bfs( array_shift( $queue ), $filter );

}

}
?>

bubblenut
11-22-2004, 01:51 PM
A little more checking could be done on the directory before you attempt to open it.
Check out file_exists(), is_dir() and is_readable(). It certainly wouldn't be faster but I think it might fall into the braket of smarter as you're far less likely to experience errors.

HTH
Bubble

stfuji
11-23-2004, 11:29 AM
The above code is updated. Thank you bubblenut for the suggestions which I have included because they are valid and applicable.

ABOVE CODE IS VALID AS OF 11/23/04

Geoffrey

stfuji
11-23-2004, 11:32 AM
A couple of fixes that might help if someone beats me to it:

1) Allow for an array of regular expressions (?)
2) Skip over un-readable directories

-Geoffrey

stfuji
11-24-2004, 06:16 PM
Be careful when using these functions.

This is a good way to load classes etc into memory if they are ordered correctly in a directory or order is not important.

I ran into an issue with a couple of configuration files that I was including.

Obviously the scope of anything included with the directory load function will be limited to the scope of the function and once the function finishes execution everything inside its scope is garbage collected.

There are some work arounds for this. Hence the following Ldap config file and classes which are loaded are call-able from anywhere. However any functions or variables need to be defined as global. i.e.:


<?php

############################################################
##
## LDAP Parameter Configuration File
##
## Copyright: Geoffrey DeFilippi / EG&G Technical Service 2004
## File: ldap.cfg.php
## Version: 1.0.1
## Date 11/04/2004
##
############################################################
##
## This structure holds the ldap configuration variables

define( 'INSTALLED_LDAP_PARAMETERS', 1 );
// flag structure declaration

if( ! isset( $_ldap ) ) {
$_ldap = new stdClass(); // Declare basic class (eg a structure)
}

global $_ldap;

$_ldap->user = ''; // anonymous login
$_ldap->pass = ''; // no password for anonymouse
$_ldap->domain = 'ldap://' . 'subdomain.domain.com';
$_ldap->protocol = 3;

?>