note 56042 deleted from function.readdir by mazzanet

From: Date: Mon, 22 Aug 2005 06:51:04 +0000
Subject: note 56042 deleted from function.readdir by mazzanet
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-94012@lists.php.net to get a copy of this message
Note Submitter: Nicolas Merlet - admin (at) merletn.org ---- Another simple recursive function to browse a tree. This function will return an indexed array containing all directories or files or both (depending on parameters). You can specify the depth you want, as explained below... <?php // $path : path to browse // $maxdepth : how deep to browse (-1=unlimited) // $mode : "FULL"|"DIRS"|"FILES" // $d : must not be defined function searchdir ( $path , $maxdepth = -1 , $mode = "FULL" , $d = 0 ) { if ( substr ( $path , strlen ( $path ) - 1 ) != '/' ) { $path .= '/' ; } $dirlist = array () ; if ( $mode != "FILES" ) { $dirlist[] = $path ; } if ( $handle = opendir ( $path ) ) { while ( false !== ( $file = readdir ( $handle ) ) ) { if ( $file != '.' && $file != '..' ) { $file = $path . $file ; if ( ! is_dir ( $file ) ) { if ( $mode != "DIRS" ) { $dirlist[] = $file ; } } elseif ( $d >=0 && $d < $maxdepth ) { $result = searchdir ( $file . '/' , $maxdepth , $mode , $d + 1 ) ; $dirlist = array_merge ( $dirlist , $result ) ; } } } closedir ( $handle ) ; } if ( $d == 0 ) { natcasesort ( $dirlist ) ; } return ( $dirlist ) ; } ?> Feel free to report any better way to do the same thing !

« previous php.notes (#94012) next »