MikroSight.com

MikroSight.com

| Home | NPA/NPX |

Google
Web
MikroSight

Create Site Map Tutorial - Page 4

Create Site Map Tutorial - Page 1
Create Site Map Tutorial - Page 2
Create Site Map Tutorial - Page 3

The next chunk of code is the main tree traversal routine. This is recursive, and it's fast too. First we open the dir named in $base - which is the parameter we pass in to this routine - typically, that will be your main site folder as seen by the file server system - specified in $sitePath. Then WHILE we can read files, determine:

  • is it a file?
  • is anything in the $ignoreIn array part of the file name?
  • is the basename in the $ignore array?
  • is the dirname in the $ignore array?
  • is the basename of the dirname in the $ignore array?
  • is the file extension in the $ignore array?

If it is a file - then if none of the other conditions are TRUE, then we want this file in our list, so we substitute the $siteUrl for the $sitePath to make this an absolute path from the internet. Then we add it to our $files array along with the last modified date for the file and show either a single ' . ' or the $file value depending on the value of $showDots.

If it's not a file - then it's a directory - as long it's not the '.' or '..' directory, then we want to "walk" it's dirTree as well - this is where the recursion occurs.

function walkDirTree($base) {
   global $files, $ignore, $sitepath, $siteurl, $showDots;

   $subdirectories=opendir($base);
   while (($subdirectory=readdir($subdirectories))!==false){
     $path=$base.$subdirectory;

     if (is_file($path)){
      if ( (!walkIgnoreIn($path))
        && (!in_array(basename($path),$ignore))
        && (!in_array(dirname($path), $ignore))
        && (!in_array(basename(dirname($path)), $ignore))
        && (!in_array(pathinfo($path, PATHINFO_EXTENSION), $ignore)) ) {

         $file = str_replace($sitepath, $siteurl, $path);
        $files[] = array('file'=>$file, 'ftime'=>filemtime($path));

        if ($showDots) {
          echo ' . ';
        }
        else {
          echo "$file<br />";
        }
      }
    }
    else {
      if (($subdirectory!='.') && ($subdirectory!='..')){
        walkDirTree($path.'/');
      }
    }
  }
} //end function walkDirTree

?>

Create Site Map Tutorial - Page 5