View Single Post
  #1 (permalink)  
Old Apr 1st, 2008, 18:43
djeyewater djeyewater is offline
Junior Member
Join Date: Oct 2007
Location: UK
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Directory listing using PHP

Hi

I am trying to write a script that makes a tree style menu out of my site's directory structure. I have the script written, and it works but I'm pretty new to PHP and would appreciate it if anyone could have a quick look at the script to see if there would be a more efficient way of doing this.

Basically the script loops through the directories and makes them a link that toggles (using js) the display of any directories below it. If a directory contains a directory (or file) named 75DPI then it instead becomes a link back to the index with the filepath to that directory appended to the URL. Hope that makes sense!

PHP: Select all

function getNav($path$i)
{
    
$nav opendir($path);
    while(
$file readdir($nav))
    {
        if(
$file != '.' && $file != '..' && $file != '300DPI')
        {
            if (
is_dir($path.'/'.$file))
            {
                
$dirContents scandir($path.'/'.$file);
                if (
in_array('75DPI'$dirContents))
                {
                    echo 
'<li><a href="index.php?path='.$path.'/'.$file.'">'.$file.'</a></li>'."\n";
                    
$i++;
                }
                else
                {
                    echo 
'<li><a href="javascript: expand(\'dir'.$i.'\');">+'.$file.'</a></li>'."\n";
                    echo 
'<ul id="dir'.$i.'">'."\n";
                    
$i++;
                    
getNav($path.'/'.$file$i);
                    echo 
'</ul>'."\n";
                }
            }
            
$i++;
        }
    }
    
closedir($nav);
}

getNav('Img'1); 

Last edited by djeyewater; Apr 2nd, 2008 at 17:06. Reason: Changing [code] tags to [php] tags
Reply With Quote