note 37188 added to function.readdir
| From: | mikael at rn2 dot php dot net | Date: | Thu, 06 Nov 2003 10:28:03 +0000 |
| Subject: | note 37188 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-59979@lists.php.net to get a copy of this message | ||
I don't know if I haven't had the patience to go trough enough notes, but I didn't
find any tip on how to recurively get a directory listing (show subdirectories too), so, as many
other, I wrote one. Hopefully this will help someone.
<?php
function direcho($path) {
if ($dir = opendir($path)) {
while (false !== ($file = readdir($dir))){
if (is_dir($path.$file)) { // if it's a dir, check
it's contents too
if ($file != '.' && $file != '..') {
// ...but dont go recursive on '.' and '..'
echo '<li><b>' . $file .
'</b></li><ul>';
direcho($path.$file); // This is a feature that surprised me, it IS
possible to call
// the function itself from inside that
function.
echo '</ul>';
}
else { //if it IS '.' or '..' , just output.
echo '<li><b>' . $file .
'</b></li>';
}
}
else { //if it's not a dir, just output.
echo '<li>' . $file . '</li>';
}
}
closedir($dir);
}
}
direcho('/path/to/home/dir/'); // somehow only the absolute path
('/home/yourhomedir/public_html/testdir/') worked for me...
?>
Yippii! my first contribution to this site!
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37188
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37188&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37188&report=yes
Search -- http://master.php.net/manage/user-notes.php