Web Design and Development Forums

Directory listing using PHP

This is a discussion on "Directory listing using PHP" within the PHP Forum section. This forum, and the thread "Directory listing using PHP are both part of the Program Your Website category.


Go Back   Webforumz.com > Program Your Website > PHP Forum

Welcome to Webforumz.com.
Register Now Register now!

Reply
 
LinkBack Thread Tools Rate Thread
Old Apr 1st, 2008, 18:43   #1 (permalink)
Junior Member
 
Join Date: Oct 2007
Location: UK
Posts: 38
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
djeyewater is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 1st, 2008, 22:48   #2 (permalink)
Nerdy Moderator
 
CloudedVision's Avatar
 
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 517
Blog Entries: 4
Re: Directory listing using PHP

its the most efficient way of doing it i can think of


Also, this is the correct way to loop through readdir (don't ask me why, it was on the PHP site.)

PHP: Select all

while(false !== ($file readdir($handle))) 

__________________
Take it easy

Other Road Design

WebForumz Moderator: HTML | Javascript | PHP
CloudedVision is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 2nd, 2008, 08:32   #3 (permalink)
Junior Member
 
Join Date: Oct 2007
Location: UK
Posts: 38
Re: Directory listing using PHP

Cool, thanks for checking it. The only reason I can see for using
PHP: Select all

while(false !== ($file readdir($handle))) 

is so it would work if you have a directory or file named 0 (since 0 evaluates to false but is not identical to false).
djeyewater is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 2nd, 2008, 15:56   #4 (permalink)
Reputable Member
 
Join Date: Nov 2007
Location: India
Posts: 150
Blog Entries: 4
Re: Directory listing using PHP

did you try using the scandir() function?But I think its available only in PHP 5 and above.
__________________
My w3 blog: w3hobbyist.com
RohanShenoy is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 2nd, 2008, 17:09   #5 (permalink)
Junior Member
 
Join Date: Oct 2007
Location: UK
Posts: 38
Re: Directory listing using PHP

I did try scandir (in fact the function uses scandir to check whether the folder contains a file or folder named '75DPI') but I couldn't figure out how to make the function work in the same way.
djeyewater is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
#1 (permalink) Free Listing To PR3 Web Design Directory! No recip! vcsonline Link Building and Link Sales 0 May 29th, 2007 13:43
Listing website Kwabena Website Planning Discussion 3 May 14th, 2007 21:14
Listing files in a folder ismilelots PHP Forum 5 Feb 15th, 2007 18:49
Business listing povidiu Link Building and Link Sales 0 Sep 21st, 2006 09:41
Listing a directory. Physt PHP Forum 7 Nov 19th, 2004 21:11



Latest Updates

All Points SEO Security Advisory - CHECK YOUR SITE NOW!

Creative Coding :: February 2008

Webforumz is sponsored by: WESH UK Web Hosting
All times are GMT. The time now is 10:54.

Sleep Study Scoring :: Free Bet :: Website Templates :: Online Betting :: Bookmakers :: Funny Quotes :: Internet Recruitment Software :: Microsoft CRM Experts :: Online Casino :: Decorated Christmas Trees :: Midwife Forums :: Football Betting :: Ecommerce Software :: Web Hosting :: Football Stats :: Dry Cleaning Collection :: xtreme wales - extreme clothing :: Apuestas :: Sharepoint Consultants :: Website Optimisation :: Office Clearance London :: Sharepoint Experts :: Sports Betting :: Casino :: Website Templates :: Web Design Development India :: Online Gambling

Powered by: vBulletin Version 3.7, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
© 2003-2008 Webforumz.com : All Rights Reserved
Search Engine Friendly URLs by vBSEO 3.2.0 RC6


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59