count,list specific file type, collapse/expand ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

count,list specific file type, collapse/expand ???

Post by Peuplarchie »

Good day to you all,
I was wondering how can i make a directory lister that would :

- count and list a specific file type "ex .html".
- Make folders collapse/expand option.

here is what I have worked out so far.

Code: Select all

 
 
 
 
<?php
 
  function CountDir($aDir, $aRecurse)
  {
    $Count = 0;
    $d = dir($aDir);
    while ($Entry = $d->Read())
    {
      if (!(($Entry == "..") || ($Entry == ".")))
      {
        if (Is_Dir($aDir . '/' . $Entry))
        {
          if ($aRecurse)
          {
            $Count += CountDir($aDir . '/' . $Entry, $aRecurse);
          }
        }
        else
        {
          $Count++;
        }
      }
    }
    return $Count;
  }
 
 
 
function getDirectory( $path = '.', $level = 0 ){
 
 
 
 
    $ignore = array( 'cgi-bin', '.', '..' );
    // Directories to ignore when listing output. Many hosts
    // will deny PHP access to the cgi-bin.
 
    $dh = @opendir( $path );
    // Open the directory to the handle $dh
    
    while( false !== ( $file = readdir( $dh ) ) ){
    // Loop through the directory
    
        if( !in_array( $file, $ignore ) ){
        // Check that this file is not to be ignored
            
            $spaces = str_repeat( '&nbsp;', ( $level * 4 ) );
            // Just to add spacing to the list, to better
            // show the directory tree.
            sort ($file);            
            $rest = substr($file, 0, -4);
 
            if( is_dir( "$path/$file" ) ){
            // Its a directory, so we need to keep reading down...
 
echo $spaces.'<a href="http://test.info/test.php?folder='.$path.'/'.$file.'" align="left" class="white00" target="image">'.$file.'</a> - '.CountDir($path.'/'.$file, true).' <br/>';
 
 
 
                getDirectory( "$path/$file", ($level+1) );
                // Re-call this same function but on a new directory.
                // this is what makes function recursive.
            
            } else {
 
echo $spaces.'<a href="http://test.info/test.php?folder='.$path.'/'.$file.'" align="left" class="white00" target="image">'.$file.'</a><br/>';
 
 
                // Just print out the filename
            
            }
        }
    }
    closedir( $dh );
    // Close the directory handle
}
 
getDirectory( "test" );
// Get contents of the "files/includes" folder  
 
?>
 
 
 
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: count,list specific file type, collapse/expand ???

Post by it2051229 »

the collapsable part, you need to learn javascript.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: count,list specific file type, collapse/expand ???

Post by aceconcepts »

Post Reply