Search code examples
phpdirectorydrop-down-menufiletree

PHP folder dropdowns


I am trying to make a dropdown menu of folders stored on a site. For example the top level would be a list of folders, and when the user hovers over the folder name, a dropdown appears so the user can see all of the files within. I'm very new to PHP and have tried a few things, but can't get the dropdowns to work. Any suggestions?

EDIT: Additional information from comments:

At the moment I'm using this, which displays the top layer, but it gets the contents of the folders to list in a dropdown, which is the problem. For the first layer I have

$thelist .= '<li><a target="frame" href="unzip/uncompressed/'.$file.'">'.$file.'</a></li>';

and can display all the contents of all the folders with

$thelist2 .= '<li><a target="frame" href="unzip/uncompressed/'.$file.'/'.$file2.'">'.$file2.'</a></li>';

I just can't get it to display in dropdowns.


Solution

  • I found the answer myself. Quote:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title>Untitled Document</title>
            <link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css">
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
            <script type="text/javascript" src="http://twitter.github.com/bootstrap/1.4.0/bootstrap-dropdown.js"></script>
        </head>
        <body>
    <?php 
    
    if ( $handle = opendir($_SERVER['DOCUMENT_ROOT'] . "/menu/") ) {
        while(false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                $var .= $file . ",";
            }
        }
    }
    
    $var = explode(',', $var);
    
    echo " <ul class='tabs'>";
    foreach($var as $v){
        if(!preg_match("/php/i", $v)) {
            echo "<li class='dropdown' data-dropdown='dropdown'><a href='" . $v . "' class='dropdown-toggle'>" . $v . "</a><ul class='dropdown-menu'>";
            $folder = $v;
            $t = opendir($_SERVER['DOCUMENT_ROOT'] . "/menu/" . $v);
            while(false !== ($v = readdir($t))) {
                if ($v != "." && $v != "..") {
                    echo "<li><a href='" . $folder ."/". $v . "'>" . $v . "</a></li>";
                }
            }
            echo "</ul></li>";
        }
    } 
    echo " </ul>";
    
    ?>
        </body>
    </html>