MikroSight.com

MikroSight.com

| Home | NPA/NPX |

Google
Web
MikroSight

Create Site Map Tutorial - Page 2

Create Site Map Tutorial - Page 1

As I mentioned, my first implementation of "Google Sitemaps" was to use one of the many site map generator sites on the web - xml-sitemaps.com. This worked fine for a while, and gave me a basic understanding of what the end result should look like.

When I started looking for ways to do this myself, googling around (for "php sitemap script"), I found several scripts to do this - my first attempt used a script from SysBotz - this script seemed cumbersome and slow, so I spent a couple of days studying the code to try and improve the performance - didn't have much luck with that, so I trashed that concept and moved on.

Here is where I decided that I wanted to walk the file system instead of the http: links. I figured that would be much faster since all the html files are there on the server anyway, why serve the pages out the http: protocol only to parse the resulting html looking for <a> tags that link back to my domain. That brought me to looking for a directory tree traversal routine for PHP - back to Googling (for "php tree traversal").

There are many of these out there too - I started with this one from Safalra. This too seemed overly complicated for my needs, so I started trimming out stuff like the $dirFunc and $afterDirFunc calls, then combined the output function into the main routine. Here's what I ended up with

<?php

$siteurl = "http://www.mikrosight.com/";
$sitepath = "/home/mikro/public_html/"; //the location of files in the server file system
$files = array();

$ignore = array ('.','..','images', 'cgi-bin','ico','Thumbs.db','jpg', 'JPG', 'gif', 'w3c', 'P3P', 'php', '.htaccess', 'css', 'txt', 'xml', 'mov', 'MOV', 'exe', 'EXE', 'ZIP', 'zip', 'mp3', 'wmv', 'asf', 'tif');

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 3