Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

773 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