note 72492 added to function.opendir
| From: | frogstarr78 at yahoo dot com | Date: | Sun, 21 Jan 2007 08:30:15 +0000 |
| Subject: | note 72492 added to function.opendir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121976@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 216.194.113.175
Probable Submitter: 68.186.90.57
----
Manual Page -- http://www.php.net/manual/en/function.opendir.php
Edit -- https://master.php.net/note/edit/72492
Del: integrated -- https://master.php.net/note/delete/72492/integrated
Del: useless -- https://master.php.net/note/delete/72492/useless
Del: bad code -- https://master.php.net/note/delete/72492/bad+code
Del: spam -- https://master.php.net/note/delete/72492/spam
Del: non-english -- https://master.php.net/note/delete/72492/non-english
Del: in docs -- https://master.php.net/note/delete/72492/in+docs
Del: other reasons-- https://master.php.net/note/delete/72492
Reject -- https://master.php.net/note/reject/72492
Search -- https://master.php.net/manage/user-notes.php