Here's a function that will recrusively turn a directory into a hash of directory hashes and file arrays, automatically ignoring "dot" files.
<?php
function hashify_directory($topdir, &$list, $ignoredDirectories=array()) {
if (is_dir($topdir)) {
if ($dh = opendir($topdir)) {
while (($file = readdir($dh)) !== false) {
if (!(array_search($file,$ignoredDirectories) > -1) && preg_match('/^\./', $file) == 0) {
if (is_dir("$topdir$file")) {
if(!isset($list[$file])) {
$list[$file] = array();
}
ksort($list);
hashify_directory("$topdir$file/", $list[$file]);
} else {
array_push($list, $file);
}
}
}
closedir($dh);
}
}
}
?>
e.g.
<?php
$public_html["StudentFiles"] = array();
hashify_directory("StudentFiles/", $public_html["StudentFiles"]);
?>
on the directory structure:
./StudentFiles/tutorial_01/case1/file1.html
./StudentFiles/tutorial_01/case1/file2.html
./StudentFiles/tutorial_02/case1/file1.html
./StudentFiles/tutorial_02/case2/file2.html
./StudentFiles/tutorial_03/case1/file2.html
etc...
becomes:
<?php
print_r($public_html);
?>
I'm using it to create a tree view of a directory.