Jump to content

Recommended Posts

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.

 




<?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( ' ', ( $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  

?>


Well there are probably loads of ways you could do this, but if you simply want to list

a specific filetype you could check out directory listing.

 

But the expand collapse? do you need to do it with PHP or could you use JavaScript?

In that case see this page expand/collapse.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.