Re: Recursive Directory Listing

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 12:13 PM -0400 10/30/08, Joe Schaeffer wrote:
I have a (readable) directory structure like so:

../navigation
    /cats
    /dogs
        /beagles
        /collies
            /some/other/dirs/
    /horses

I need to display those directories in nested html lists (marked up
with css). Using PHP on the above, I'd like to produce the following
HTML:

<ul>
    <li>cats</li>
    <li>dogs
        <ul>
            <li><beagles></li>
            <li><collies>
                <ul><li>some...</li></ul>
            </li>
        </ul>
    </li>
    <li>horses</li>
</ul>

--joe:

The html is simple to add, so I'll provide just the basics of placing a directory and it's contents in an array:

<?php
echo '<pre>';
print_r(recur_dir('.'));
echo '</pre>';
?>


<?php // === functions ==========

function recur_dir($dir)
{
$dirlist = opendir($dir);
while ($file = readdir ($dirlist))
{
if ($file != '.' && $file != '..')
{
$newpath = $dir.'/'.$file;
$level = explode('/',$newpath);
if (is_dir($newpath))
{
$mod_array[] = array(
'level'=>count($level)-1,
'path'=>$newpath,
'name'=>end($level),
'kind'=>'dir',
'mod_time'=>filemtime($newpath),
'content'=>recur_dir($newpath));
}
else
{

$mod_array[] = array(
'level'=>count($level)-1,
'path'=>$newpath,
'name'=>end($level),
'kind'=>'file',
'mod_time'=>filemtime($newpath),
'size'=>filesize($newpath));
}
}
}
closedir($dirlist);
return $mod_array;
}

Cheers,

tedd

PS: This is nathan's code.

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux