PHP构建图库导航

I was wondering if it is possible to use my server's file structure to automatically build a navigation menu for an image gallery. Currently I have a simple "showcase" with hard-coded links to different folders of images (using jquery and ajax and php, some things that I don't quite understand but learned how to use from tutorials and the like). Basically, I have three files:

  • main.php
  • main.css
  • images.php

and I use hard links on main.php to call the images.php script to load a specific folder containing images into a div on the main page.

Here is my current nav setup:

 <ul>
    <li><a href="#" onclick="loadPage('images.php?dirname=images/animals')">Animals</a></li>
    <li><a href="#" onclick="loadPage('images.php?dirname=images/people')">People</a></li>
    <li><a href="#" onclick="loadPage('images.php?dirname=images/objects')">Objects</a></li>
</ul>

My question is: Since all my images are in subdirs under the "images" dir, is there a way I can just build the navigation points (php script?) using the names of the subdirs in "images"? (such that it is kept up to date when I add more folders)

also, for some reason I can't make the variables on my script include the 'images.php?dirname=images/', is there any way to fix that?

If they are all in the images directory, you can specify a $image_path

<?php
$image_path = '/full/path/to/images';
if ($_GET['gallery']) {
    $gallery_path = $image_path . '/' . $_GET['gallery'];

    # if $_GET['gallery'] is `animals`:
    # 
    # $gallery_path = '/full/path/to/images/animals'

    # load your images within this path
}
?>

And to get all the subdirectories use dir

<?php
$image_path = '/full/path/to/images';
$d = dir($image_path);
while (false !== ($entry = $d->read())) {
    if (is_dir($image_path . $entry)) {
        if (($entry != '.') || ($entry != '..')) {
            echo $entry; # or print your html code for each directory.
        }
    }
}
?>