Solved

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

Posted on 2008-06-15
3
272 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

860 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