Files and folders in treeview

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I'm trying to get to view files and folders in the current directory in Hierarchical Treeview. ANy help on this would be really gladly taken and appreciated :D
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

You'll need a recursive directory scanning function.
You'll use nested <ul> elements.

You'll combine the two so that the recursive directory function outputs parts of the treeview as it works.

You'll likely include CSS classnames on <li> elements along the lines of "folder open", "folder closed", "file text", "file php" etc.
Yes I meant to use spaces in those classnames.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I tried something like the following...
PHP Code: [ Select ]
<?php
class gen_tree
{
 
   var $in_dir = false;
 
   function gen_tree($dir = './')
   {
      foreach(new DirectoryIterator($dir) as $file)
      {
         if ((!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])))
         {
            if($file->isDir())
            {
               $this->in_dir = true;
               $this->gen_tree($file->getFilename);
            }
            else
            {
               if($this->in_dir == true)
               {
                  echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
               }
               echo '<a href="'. $thisPage .'?file='. $dir .'/'. $file->getFilename() .'&amp;dir='. $dir .'">'. $file->getFilename() .'</a><br />'."\n";
            }
         }
      }
   }
}
 
new gen_tree();
?>
  1. <?php
  2. class gen_tree
  3. {
  4.  
  5.    var $in_dir = false;
  6.  
  7.    function gen_tree($dir = './')
  8.    {
  9.       foreach(new DirectoryIterator($dir) as $file)
  10.       {
  11.          if ((!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])))
  12.          {
  13.             if($file->isDir())
  14.             {
  15.                $this->in_dir = true;
  16.                $this->gen_tree($file->getFilename);
  17.             }
  18.             else
  19.             {
  20.                if($this->in_dir == true)
  21.                {
  22.                   echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  23.                }
  24.                echo '<a href="'. $thisPage .'?file='. $dir .'/'. $file->getFilename() .'&amp;dir='. $dir .'">'. $file->getFilename() .'</a><br />'."\n";
  25.             }
  26.          }
  27.       }
  28.    }
  29. }
  30.  
  31. new gen_tree();
  32. ?>

But that is a slow method and all of the folders are open, and you can't close them and... it's just not good. I had it working with Folder images in front of folders and file images in front of files, I just deleted the code yesterday when I found some code there, but it wasn't what I wanted...

I'll try something, and if it doesn't work, I'll come back here and post it.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

What Joe said.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Alright... I got the following "working" (The JavaScript is being stubborn).
Code: [ Select ]
<html>
    <head>
        <title>TreeView Test</title>
        <style type="text/css">
            .dir {
                list-style-image: url('folder.png');
            }
            
            .file {
                list-style-image: url('page.png');
            }
        </style>
        <script language="JavaScript">
            function showAndHide(theId)
            {
                var el = document.getElementById(theId)

                if (el.style.display == "none")
                {
                    el.style.display = "block";
                }
                else
                {
                    el.style.display = "none";
                }
            }
        </script>
    </head>
<body>
<?php
function getDirectory($path = '.')
{
    // Directories to ignore when listing output. Many hosts
    // will deny PHP access to the cgi-bin.
    $ignore = array('cgi-bin', '.', '..');
    $i = 0;
    // Open the directory to the handle $dh
    $dh = @opendir($path);
    echo "<ul>\n";
    // Loop through the directory
    while(false !== ($file = readdir($dh)))
    {
        // Check that this file is not to be ignored
        if(!in_array($file, $ignore))
        {
            // Its a directory, so we need to keep reading down...
            if(is_dir("$path/$file"))
            {
                // Re-call this same function but on a new directory.
                // this is what makes function recursive.
                echo "<li class=\"dir\"><a href=\"#\" onclick=\"showAndHide(dir$i);\">$file</a></li>\n";

                echo "<ul style=\"display: none;\" id=\"dir$i\">\n";
                getDirectory("$path/$file");
                echo "</ul>\n";
            }
            else
            {
                // Just print out the filename
                echo "<li class=\"file\"><a href=\"$path/$file\">$file</a></li>\n";
            }
        }
        ++$i;
    }
    echo "</ul>\n";
    // Close the directory handle
    closedir( $dh );
}

getDirectory();
?>
</body>
</html>
  1. <html>
  2.     <head>
  3.         <title>TreeView Test</title>
  4.         <style type="text/css">
  5.             .dir {
  6.                 list-style-image: url('folder.png');
  7.             }
  8.             
  9.             .file {
  10.                 list-style-image: url('page.png');
  11.             }
  12.         </style>
  13.         <script language="JavaScript">
  14.             function showAndHide(theId)
  15.             {
  16.                 var el = document.getElementById(theId)
  17.                 if (el.style.display == "none")
  18.                 {
  19.                     el.style.display = "block";
  20.                 }
  21.                 else
  22.                 {
  23.                     el.style.display = "none";
  24.                 }
  25.             }
  26.         </script>
  27.     </head>
  28. <body>
  29. <?php
  30. function getDirectory($path = '.')
  31. {
  32.     // Directories to ignore when listing output. Many hosts
  33.     // will deny PHP access to the cgi-bin.
  34.     $ignore = array('cgi-bin', '.', '..');
  35.     $i = 0;
  36.     // Open the directory to the handle $dh
  37.     $dh = @opendir($path);
  38.     echo "<ul>\n";
  39.     // Loop through the directory
  40.     while(false !== ($file = readdir($dh)))
  41.     {
  42.         // Check that this file is not to be ignored
  43.         if(!in_array($file, $ignore))
  44.         {
  45.             // Its a directory, so we need to keep reading down...
  46.             if(is_dir("$path/$file"))
  47.             {
  48.                 // Re-call this same function but on a new directory.
  49.                 // this is what makes function recursive.
  50.                 echo "<li class=\"dir\"><a href=\"#\" onclick=\"showAndHide(dir$i);\">$file</a></li>\n";
  51.                 echo "<ul style=\"display: none;\" id=\"dir$i\">\n";
  52.                 getDirectory("$path/$file");
  53.                 echo "</ul>\n";
  54.             }
  55.             else
  56.             {
  57.                 // Just print out the filename
  58.                 echo "<li class=\"file\"><a href=\"$path/$file\">$file</a></li>\n";
  59.             }
  60.         }
  61.         ++$i;
  62.     }
  63.     echo "</ul>\n";
  64.     // Close the directory handle
  65.     closedir( $dh );
  66. }
  67. getDirectory();
  68. ?>
  69. </body>
  70. </html>

Any help here? It's getting quite annoying... The JavaScript isn't working...
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Well, I got what I was wanting a different way then a hierarchical treeview thing. What I was doing a Homepage for XAMPP like the one WAMP had... I got it done though a bit differently, but I'm still happy with it :)

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 87 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.