Voting

: max(two, nine)?
(Example: nine)

The Note You're Voting On

frogstarr78 at yahoo dot com
18 years ago
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);
/*
outputs:
array(
"StudentFiles" => array (
"tutorial_01" => array (
"case1" => array( "file1.html", "file2.html")
),
"tutorial_02" => array (
"case1" => array( "file1.html"),
"case2" => array( "file2.html")
),
"tutorial_03" => array (
"case1" => array( "file2.html")
)
)
)
*/
?>
I'm using it to create a tree view of a directory.

<< Back to user notes page

To Top