Dynamic PHP directory navigation

  • aloof
  • Newbie
  • Newbie
  • aloof
  • Posts: 14
  • Loc: Oregon

Post 3+ Months Ago

I'm looking for some scripts or pseudo-code or algorithms for PHP that will help me with the following:

1) Dynamic listing of directories as a form of navigation for a website.

2) The navigation bar on the left side lists hard-coded links. Each link is tied to a directory, when a link is clicked, the index.php for that directory is loaded and the navigation panel lists the hard-coded links + the link clicked expanded listing the folders in that directory.

3) Multiple-depths allowed, meaning I can click a hard-coded link, it will drop down its sub-folders, I can click one of those, and have those drop down, etc. etc. After the 1st level both folders and files would be listed, not just folders.

Anybody help me with pseudo-code/algorithms and even some functions or scripts? I've googled around but can't quite seem to find what I'm looking for.

Thanks in advance.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • guitrspaz
  • Graduate
  • Graduate
  • guitrspaz
  • Posts: 106
  • Loc: Baltimore, MD

Post 3+ Months Ago

I use this to display all folders and files in my local root folder it might work for you with some mods:

Code: [ Select ]
$dot = ".";
if ($handle = opendir('./')) {
$x = '0';
  while (false !== ($file = readdir($handle))) {
  if (($file != "..") AND ($file != '.') AND ($file != 'cgi-bin') AND ($file != '.DS_Store')) {
    echo "<tr><td><a href=$file>$file</a></td></tr>\n\n";
         $x++;
         }
  }
  closedir($handle);
}
if ($x <= '0') {
    echo "<tr><td>no projects</td></tr>";
}
  closedir($handle);
}
  1. $dot = ".";
  2. if ($handle = opendir('./')) {
  3. $x = '0';
  4.   while (false !== ($file = readdir($handle))) {
  5.   if (($file != "..") AND ($file != '.') AND ($file != 'cgi-bin') AND ($file != '.DS_Store')) {
  6.     echo "<tr><td><a href=$file>$file</a></td></tr>\n\n";
  7.          $x++;
  8.          }
  9.   }
  10.   closedir($handle);
  11. }
  12. if ($x <= '0') {
  13.     echo "<tr><td>no projects</td></tr>";
  14. }
  15.   closedir($handle);
  16. }
  • aloof
  • Newbie
  • Newbie
  • aloof
  • Posts: 14
  • Loc: Oregon

Post 3+ Months Ago

The following is my function code...can anybody help me with an implementation algorithm that might accomplish what I previously stated? The way the function works will it even allow for what I want?

Code: [ Select ]
<?php

/**
* dirtree
* recursivly reads all of the files and prints them out all dynamic like
*
* @param:
*   - $path - the directory we're currently reading
*   - $depth - the maximum depth of the search
*/
function dirtree($path,$depth,$wkdir)
{
     // if there's a slash at the end of the path then remove it
    if(substr($path,strlen($path)-1,1) == "/")
     {
         $path = substr($path,0,strlen($path)-1);
     }

     // the directory handle
     $dh = opendir($path);

     // while there are still directories to read
     while (false !== ($filename = readdir($dh)))
     {
          // if the file begins with a dot, (. or .. or .htaccess, etc.) then don't read it in
          if(substr($filename,0,1) != ".")
          {
              // if what we're reading is a directory the add it to an array of directories
               // so we can sort them and list them first
               if(is_dir($path."/".$filename))
               {
                   $dirs[] = $filename;
               }
          }
    }

     // if there were directories, sort them
     if($dirs)
     {
         sort($dirs);
     }

     // directories
     if($dirs)
     {
          // for each directory, except the last one
          for($i=0;$i<count($dirs)-1;$i++)
          {
               $dirname = strtr($dirs[$i],"_"," ");

               echo "<a href='".$wkdir.$dirs[$i]."' class='source'>&nbsp;".$dirname."</a>";
    
               // recursively call the function to list all of the directories contents first
               // because we're looking at directores first, then files
               if ($depth)
               {
                dirtree($path."/".$dirs[$i],$depth-1);
               }
          }

          //echo the last directory

           $i = count($dirs)-1;
           $dirname = strtr($dirs[$i],"_"," ");
           echo "<a href='".$wkdir.$dirs[$i]."' class='source'>&nbsp;".$dirname."</a>";

           if ($depth)
           {
                dirtree($path."/".$dirs[$i],$depth-1);
           } // if($depth)
     } // if($dirs)
}

?>
  1. <?php
  2. /**
  3. * dirtree
  4. * recursivly reads all of the files and prints them out all dynamic like
  5. *
  6. * @param:
  7. *   - $path - the directory we're currently reading
  8. *   - $depth - the maximum depth of the search
  9. */
  10. function dirtree($path,$depth,$wkdir)
  11. {
  12.      // if there's a slash at the end of the path then remove it
  13.     if(substr($path,strlen($path)-1,1) == "/")
  14.      {
  15.          $path = substr($path,0,strlen($path)-1);
  16.      }
  17.      // the directory handle
  18.      $dh = opendir($path);
  19.      // while there are still directories to read
  20.      while (false !== ($filename = readdir($dh)))
  21.      {
  22.           // if the file begins with a dot, (. or .. or .htaccess, etc.) then don't read it in
  23.           if(substr($filename,0,1) != ".")
  24.           {
  25.               // if what we're reading is a directory the add it to an array of directories
  26.                // so we can sort them and list them first
  27.                if(is_dir($path."/".$filename))
  28.                {
  29.                    $dirs[] = $filename;
  30.                }
  31.           }
  32.     }
  33.      // if there were directories, sort them
  34.      if($dirs)
  35.      {
  36.          sort($dirs);
  37.      }
  38.      // directories
  39.      if($dirs)
  40.      {
  41.           // for each directory, except the last one
  42.           for($i=0;$i<count($dirs)-1;$i++)
  43.           {
  44.                $dirname = strtr($dirs[$i],"_"," ");
  45.                echo "<a href='".$wkdir.$dirs[$i]."' class='source'>&nbsp;".$dirname."</a>";
  46.     
  47.                // recursively call the function to list all of the directories contents first
  48.                // because we're looking at directores first, then files
  49.                if ($depth)
  50.                {
  51.                 dirtree($path."/".$dirs[$i],$depth-1);
  52.                }
  53.           }
  54.           //echo the last directory
  55.            $i = count($dirs)-1;
  56.            $dirname = strtr($dirs[$i],"_"," ");
  57.            echo "<a href='".$wkdir.$dirs[$i]."' class='source'>&nbsp;".$dirname."</a>";
  58.            if ($depth)
  59.            {
  60.                 dirtree($path."/".$dirs[$i],$depth-1);
  61.            } // if($depth)
  62.      } // if($dirs)
  63. }
  64. ?>

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 70 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.