Listing folders in list box, folder only, recursively

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Listing folders in list box, folder only, recursively

Post by Peuplarchie »

Good day to you all,

I'm working on a piece of code which list directory into a dropdown box.
No problem, with that :

Code: Select all

 
 
 
 
<p>List Box - Single Select<br>
<select name="listbox" size="1">
<option  selected>Click here to see them by themes</option>
<?PHP
 
if ($handle = opendir('themes/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != '.' && $file != '..') {
            $fileName = str_replace('.mov', '', $file);
            echo '<option value="' . $file . '">' . $fileName . '</option>';
        }
    }
    closedir($handle);
}
 
 
 
 
?>
 
</select>
</p>
 
 


Now what I would like to do is :

1- Read the directory recursively .


Thanks !
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Listing folders in list box, folder only, recursively

Post by McInfo »

Is this the same project you were working on a few weeks ago?

Maybe you can get something figured out with this. It's been a while since I wrote it. Sorry for the lack of comments.

Code: Select all

<?php
header('Content-Type: text/plain');
 
rdir('C:/dev/xampp/htdocs/forums', 2);
 
function rdir ($directory = '.', $max_depth = 1, $indent = ".\t", $depth = 0)
{
    $basename = basename($directory);
   
    if (is_file($directory))
    {
        echo PHP_EOL.str_repeat($indent, $depth).$basename;
        return true;
    }
    elseif (is_dir($directory))
    {
        echo PHP_EOL.str_repeat($indent, $depth).'['.$basename.']';
       
        if ((0 !== strpos($basename, '.') || 0 == $depth)
        && $max_depth > $depth++ && ($dh = @opendir($directory)))
        {
            while (false !== ($file = readdir($dh)))
            {
                rdir("$directory/$file", $max_depth, $indent, $depth);
            }
            closedir($dh);
        }
        return true;
    }
    else
    {
        return false;
    }
}
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 1:04 pm, edited 1 time in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Listing folders in list box, folder only, recursively

Post by McInfo »

Have you really been working on this since 2006? I admire your commitment.

Edit: This post was recovered from search engine cache.
Post Reply