Dynamic Site Map with PHP

This is a script, written in PHP, that will generate a site map based on the directory structure of the site. You can easily exclude files and/or directories.


The site map is an expandable list, similar to Windows Explorer and is accessible with or without JavaScript enabled on the user's browser. If JavaScript is not enabled, the list is simply displayed in its expanded format so all links are visible. Directories and files are sorted alphabetically with directories listed first. Note that the site map is generated from the file system, not the links contained in the pages.

You can see an example of the site map in action by viewing this site's site map. You are invited to try that page with JavaScript enabled and with JavaScript disabled to see how it works.

How it works

The script functions by recursively calling itself to traverse the directory tree starting at the site's root directory. It gets the file title from each file on the site and displays the page title, with a link to the file. Note that the script obtains the title of the page by extracting the text between the opening and closing <title> tags. That means that it only works on pages that exist (not all the variations that can be generated) and where the title is static and contained in the page.

As you might imagine, this makes the script rather processor intensive. However, for smaller sites, it doesn't present much of a problem. I've used the script on a site of around 500 pages and it has a slight delay, but not so much as to be objectionable.

For larger sites or sites where the number of pages and their titles don't change frequently, you can run the script once on your development machine, then view the browser's source code and paste the result into a static page. That eliminates the processor load, but does require you to manually update the site map each time you add, delete, or rename a page.


You can download the files necessary. Configuration is easy. See the readme.txt file included in the ZIP file for details.