note 37188 deleted from function.readdir by tularis
| From: | tularis@php.net | Date: | Sun, 13 May 2007 19:06:27 +0000 |
| Subject: | note 37188 deleted from function.readdir by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126549@lists.php.net to get a copy of this message | ||
Note Submitter: mikael[at]limpreacher[d0t]com
----
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!