Solved

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

Posted on 2008-06-15
3
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 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