list folders script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 7effrey
    New Member
    • Feb 2008
    • 12

    list folders script

    Hi

    Does anyone know how to make a script that list the folders in a specific directory, but when the list is made it must be possible to click on it like a link so people can be redirected, but not to the folder, but to a special file inside the folder.

    example:
    - list the names on the folders from root
    - the names appear in a link form (<a href=' ' </a>) on my php page
    - when i click i shall be redirected to one-of-the-folder-names/index.php

    thank you for paying attention, i hope you can help
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Happen to have one of those 'on-the-shelf'
    [php]
    <?php
    // Your start directory
    getDirectory('./');

    function getDirectory( $path = '.', $level = 0 )
    {
    // Directories to ignore when listing output.
    $ignore = array( '.', '..' );

    // Open the directory to the handle $dh
    $dh = @opendir( $path );

    // Loop through the directory
    while( false !== ( $file = readdir( $dh ) ) )
    {
    // Check that this file is not to be ignored
    if( !in_array( $file, $ignore ) )
    {
    // Indent spacing for better view
    $spaces = str_repeat( '&nbsp;', ( $level * 5 ) );

    // Show directories only
    if(is_dir( "$path/$file" ) )
    {
    // Re-call this same function but on a new directory.
    // this is what makes function recursive.
    echo "$spaces<a href='$path/$file/index.php'>$fil e</a><br />";
    getDirectory( "$path/$file", ($level+1) );
    }
    }
    }
    // Close the directory handle
    closedir( $dh );
    }
    ?>
    [/php]
    Ronald

    Comment

    • 7effrey
      New Member
      • Feb 2008
      • 12

      #3
      Originally posted by ronverdonk
      Happen to have one of those 'on-the-shelf'
      [php]
      <?php
      // Your start directory
      getDirectory('./');

      function getDirectory( $path = '.', $level = 0 )
      {
      // Directories to ignore when listing output.
      $ignore = array( '.', '..' );

      // Open the directory to the handle $dh
      $dh = @opendir( $path );

      // Loop through the directory
      while( false !== ( $file = readdir( $dh ) ) )
      {
      // Check that this file is not to be ignored
      if( !in_array( $file, $ignore ) )
      {
      // Indent spacing for better view
      $spaces = str_repeat( '&nbsp;', ( $level * 5 ) );

      // Show directories only
      if(is_dir( "$path/$file" ) )
      {
      // Re-call this same function but on a new directory.
      // this is what makes function recursive.
      echo "$spaces<a href='$path/$file/index.php'>$fil e</a><br />";
      getDirectory( "$path/$file", ($level+1) );
      }
      }
      }
      // Close the directory handle
      closedir( $dh );
      }
      ?>
      [/php]
      Ronald
      Hi again

      i noticed this:
      // Directories to ignore when listing output.
      $ignore = array( '.', '..' );

      using this script as provided shows some of my folders which lies inside the folder which names is displayed. What is the kind of "code" that i have to add to $ignore = array( '.', '..' ); to ignore showing these folders?

      Example:
      Name-on-folder
      - foldder-inside-Name-on-folder


      Noticed that adding "folder-inside" will remove showing the folder, but because i have so many folders, i want to write a generel thing that ignores all the folders
      $ignore = array( '.', '..', "folder-inside" );

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Just take out the recursive part, i.e. line 28
        [php]getDirectory( "$path/$file", ($level+1) ); [/php]
        anf it will only show the folders under the dir you specified.

        Ronald

        Comment

        • imran007
          New Member
          • Jul 2008
          • 2

          #5
          Hi,

          Is there a way to display the listing alphabetically?

          Regards
          Imran

          Comment

          • coolsti
            Contributor
            • Mar 2008
            • 310

            #6
            Nice little script. Would be only a small amount of effort to modify it (with a third argument) to let the user specify how many levels down in the directory tree the function (and it's recursive calls) would go.

            To sort the results, I would suppose I would modify all the echo statements so that instead of directly printing out the results to the remote user as the "dh" list is retrieved, I would store the results intermediately in arrays. Then when all the files and subdirectories have been traversed, sort the arrays one by one alphabetically and do the "echo" printouts to the user.

            Comment

            • imran007
              New Member
              • Jul 2008
              • 2

              #7
              Originally posted by coolsti
              Nice little script. Would be only a small amount of effort to modify it (with a third argument) to let the user specify how many levels down in the directory tree the function (and it's recursive calls) would go.

              To sort the results, I would suppose I would modify all the echo statements so that instead of directly printing out the results to the remote user as the "dh" list is retrieved, I would store the results intermediately in arrays. Then when all the files and subdirectories have been traversed, sort the arrays one by one alphabetically and do the "echo" printouts to the user.
              Do you any sample code I can use?

              Comment

              • richard11259375
                New Member
                • Jan 2010
                • 1

                #8
                Thanks for sharing your script, Roverdonk. How do I get it to list the folders / links in reverse order, so that the newest is on the top?

                Thanks.

                R

                Comment

                • dlite922
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1586

                  #9
                  see php.net/glob

                  and php.net/filemtime

                  get the directory files with glob. store the filename and the file modified time in an array. Then reverse sort the array: php.net/arsort

                  Isn't PHP great? The manual gives you all these fantastic functions to do anything you want.



                  Dan

                  Comment

                  Working...