Closed Thread
List files in directory and sub directories
Old 01-11-2006, 08:28 AM List files in directory and sub directories
RikComery's Avatar
Average Talker

Posts: 18
Location: Essex, United Kingdom
Trades: 0
Hi. I am looking for a script that will simply list the file name of every file in a certain directory, and all it's sub directories. Is this possible, and does anyone know how to do it?
RikComery is offline
View Public Profile Visit RikComery's homepage!
 
 
When You Register, These Ads Go Away!
Old 01-11-2006, 09:45 AM
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
yes and yes

PHP Code:
function ListFiles($dir) {

    if(
$dh opendir($dir)) {

        
$files = Array();
        
$inner_files = Array();

        while(
$file readdir($dh)) {
            if(
$file != "." && $file != ".." && $file[0] != '.') {
                if(
is_dir($dir "/" $file)) {
                    
$inner_files ListFiles($dir "/" $file);
                    if(
is_array($inner_files)) $files array_merge($files$inner_files); 
                } else {
                    
array_push($files$dir "/" $file);
                }
            }
        }

        
closedir($dh);
        return 
$files;
    }
}


foreach (
ListFiles('/home/ibbo') as $key=>$file){
    echo 
$file ."<br />";

This method useses recursion (it calls itself untill happy basically) to filter all the files in a given dir. If file returns as type dir then we enter it and poll all the files again untill we find no more dir's. We then drop back a dir and move to the next file repeating the process when we find another dir.

If file is not a dir we just append it to our list which we print out later.

Easy for some, confusing for others but likely to be behond most.

Ibbo
__________________
www.nationalclubgolfer.com www.sportspub.co.uk www.bespokecc.co.uk www.centralmarquees.co.uk
Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf

Last edited by ibbo; 01-11-2006 at 09:49 AM..
ibbo is offline
View Public Profile Visit ibbo's homepage!
 
Closed Thread     « Reply to List files in directory and sub directories
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.09784 seconds with 14 queries