My PHP Navigation Script

Description

This script populates the blue navigation area on all of my pages except for the main index page. It is released with a GPL v3 licence without warranty. You can modify it to your liking and experiment with it on your site.

It dynamically lists the files and subfolders of the directory it is currently occupying.

It is embedded in a .php file using the include directive.



<?php
function showRegularFiles($path)
{
    $fileList = array();
    if($handle = opendir($path))
    {
        while (false !== ($file = readdir($handle)))
        {
            if (!is_dir($file) && $file != 'index.php'
                && $file != 'navigation.php' && $file[0] != '.')
            {
               // echo '<a href = "'.$file.'">'."$file"."</a><br/>\n";
                array_push($fileList, $file);
            }
        }
        closedir($handle);
    }
    sort($fileList);
    foreach($fileList as $k)
    {
        echo '<li><a href = "'.$k.'">'."$k"."</a></li>\n";
    }
    unset($k);
}

function showDirectories($path)
{
    $dirList = array();
    if($handle = opendir($path))
    {
        while (false !== ($file = readdir($handle)))
        {
            if (is_dir($file) && $file != "." && $file != "..")
            {
               // echo '<a href = "'.$file.'">'."$file"."</a><br/>\n";
                array_push($dirList, $file);
            }
        }
        closedir($handle);
    }
    sort($dirList);
    foreach($dirList as $k)
    {
        echo '<li><a href = "'.$path."/".$k.'">'."$k"."</a></li>\n";
    }
    unset($k);
}
?>

<h2>Navigation</h2>

<h3>Directories</h3>

<?php
echo '<ul>';
echo '<li><a href = "..">Parent Directory</a></li>';
showDirectories(".");
echo '</ul>';
echo '<h3>Files</h3>';
echo '<ul>';
showRegularFiles(".");
echo '</ul>';
echo '<h3>Other Places</h3>';
echo '<ul>';
echo '<li><a href = "index.php">Back to Index Page</a></li>';
echo '<li><a href = "/index.php">Main Index Page</a></li>';
echo '</ul>';
?>