Solved

How to scan all directories and sub-directories and print their name with numbering & link to them?

Posted on 2008-06-15
3
244 Views
Last Modified: 2010-04-21
Hi

You will code "index.php" which will scan the directory in which it resides for all other directories and sub directories. It should print  one directory in oen line and then it should print no. of files (specific files like .mp3) in it. It should be well formatted so that we can easily understand that this is dun directory of this directory.
0
Comment
Question by:theredcode
  • 2
3 Comments
 

Author Comment

by:theredcode
ID: 21788055
How to pass arguments to this function and how call it to print output...



// ------------ lixlpixel recursive PHP functions -------------
// scan_directory_recursively( directory to scan, filter )
// expects path to directory and optional an extension to filter
// of course PHP has to have the permissions to read the directory
// you specify and all files and folders inside this directory
// ------------------------------------------------------------

function scan_directory_recursively($directory, $filter)
{
  // if the path has a slash at the end remove it here
  if(substr($directory,-1) == '/')
  {
    $directory = substr($directory,0,-1);
  }

  // if the path is not valid or is not a directory ...
  if(!file_exists($directory) || !is_dir($directory))
  {
    // ... return false and exit the function
    return FALSE;

    // ... else if the path is readable
  }
  elseif(is_readable($directory))
  {
  // open the directory
  $directory_list = opendir($directory);

  // and scan through the items inside
  while (FALSE !== ($file = readdir($directory_list)))
  {
    // if the filepointer is not the current directory
    // or the parent directory
    if($file != '.' && $file != '..')
    {
      // build the new path to scan
      $path = $directory.'/'.$file;

      // if the path is readable
      if(is_readable($path))
      {
        // split the new path by directories
        $subdirectories = explode('/',$path);
        // save the folders name
        $folders_name = substr($subdirectories, $path);
        // if the new path is a directory
        if(is_dir($path))
        {
          // save the folders name
          $folders_name = substr($subdirectories, $path);
          // add the directory details to the file list
          $directory_tree[] = array('path' => $path,
                                    'name' => end($subdirectories),
                                    'kind' => 'directory',
                                    // scan the new path by calling this function
                                    'content' => scan_directory_recursively($path, $filter));

        // if the new path is a file
        }
        elseif(is_file($path))
        {
          // get the file extension by taking everything after the last dot
          $extension = end(explode('.', end($subdirectories)));

          // if there is no filter set or the filter is set and matches
          if($filter === false || $filter == $extension)
          {
            // add the file details to the file list
            $directory_tree[] = array('path' => $path,
                                      'name' => end($subdirectories),
                                      'extension' => $extension,
                                      'size' => filesize($path),
                                      'kind' => 'file');
          }
        }
      }
    }
  }
  // close the directory
  closedir($directory_list);

  // return file list
  return $directory_tree;

  // if the path is not readable ...
  }
  else
  {
    // ... we return false
    return false;
  }
}

?>
0
 
LVL 4

Accepted Solution

by:
afzz earned 500 total points
ID: 21788061
TRy the following
<?php

function showDirect( $pth = '.', $lev = 0 ){

    $ignore = array( 'cgi-bin', '.', '..' );// Directories to ignore

    $dh = @opendir( $pth );

    while( false !== ( $file = readdir( $dh ) ) ){

        if( !in_array( $file, $ignore ) ){

            $spacer = str_repeat( '&nbsp;', ( $lev * 4 ) );

            if( is_dir( "$pth/$file" ) ){

                echo "<strong>$spacer $file</strong><br />";

                showDirect( "$pth/$file", ($lev+1) );            

            } else {

                echo "$spacer $file<br />";            

            }

        }

    }

    closedir( $dh );

}
 

?> 
 
 

Calling the function
 
 

showDirect( "." );// Current directory
 

showDirect( "./this/directory" );// Show "this/directory"

Open in new window

0
 

Author Closing Comment

by:theredcode
ID: 31467316
thnx
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now