PHP发布指向子目录和php的链接以显示图像

I'm very basic when it comes to PHP.

With my website, I have a directory called "uploads"

Within "uploads" I have 3 folders "Example1" "Example2" and "Example3"

Within each of the folders, they contain images.

I need to know how to use php to create a navigation for every sub directory. So that if I add a new folder "Example4" it will give a navigation like:

Select what section you're looking for:

  Example1 | Example2 | Example3

and if I later add new folders add them to the navigation. EX:

  Example1 | Example2 | Example3 | Example4 | Example5

Then once they click the link to go into the folder, have a code that displays all the images in that folder.

So far I have:

<?php
$files = glob("uploads/*.*");
for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
echo '<a href="/'.$num.'"><img src="/'.$num.'"></a>'."<p>";
}
?>

but it will only display the images in the upload directory, not the images in Example1 and so on.

How on earth would I go about doing this? I'm doing it for a school project and have two weeks to complete it, but I am so lost. I only have knowledge with CSS, HTML, and the only PHP I know is php includes, so any help would be appreciated.

To get files of every specific folder ,pass it throw a get variable that contains folder's name,an then scan this folder an show images ,url should be like this :

 listImages.php?folderName=example1

To have menu like what you want :

<?php
    $path = 'uploads/' ;
    $results = scandir($path);
    for ($i=0;$i<count($results);$i++   ) {
        $result=$results[$i];
        if ($result === '.' or $result === '..') continue;

        if (is_dir($path . '/' . $result)) {
             echo "<a href='imgs.php?folderName=$result'>$result</a>  ";

        }
        if($i!=count($results)-1) echo '|'; //to avoid showing | in the last element
    }
?>

And here is PHP page listImages that scan images of a specific folder :

<?php
    if (isset($_GET['folderName'])) $folder=$_GET['folderName'];
    $path = 'uploads/'.$folder.'/' ;
    $images = glob($path . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    foreach ($images as $image) {
         echo "<img src='$image' />";
    }
?>

First of all, do read more PHP manual, for directory related: opendir, for files related: fopen

The following code is basically re-arranging the example code provided in opendir. What it does:

A scan_directory function to simply check if directory path is valid and is a directory, then proceed to do a recursive call if there's a child directory else just print out the file name.

The first if/else condition is just to ensure the base directory is valid.

I'll added ul and li to make it slightly more presentable.

$base_dir = 'upload';

if (is_dir($base_dir)) 
    scan_directory($base_dir);
else 
    echo 'Invalid base directory. Please check your setting.';

// recursive function to check all dir
function scan_directory($path) {
    if (is_dir($path)) {
        if ($dir_handle = opendir($path)) {
            echo '<ul>';
            while (($file = readdir($dir_handle)) !== false) {
                if ($file != '.' && $file != '..') {
                    if (is_dir($path . '/' . $file)) {
                        echo '<li>';
                        echo $file;
                        scan_directory($path . '/' . $file);
                        echo '</li>';
                    }
                    else
                        echo "<li>{$file}</li>";
                }
            }
            echo '</ul>';
        }
    }
}

create image as subdirectory name with image name and save it in database

example:

subdirectory name: example2

image name: image.jpg

store image name in db as "example2/image.jpg"

Since it seems that you are familiar with globs a bit, here is an example using the "glob" function. You can see a basic working example of what you are looking for here: http://newwebinnovations.com/glob-images/

Here is how I have the example set up:

There are two PHP files, one is index.php and the other is list-images.php.

There is also a folder for images two subfolders that have images inside of them.

index.php is the file that finds the folders in the images folder and places them in a list with links list-images.php which will display the images inside of the folder:

$path = 'images';
$folders = glob($path.'/*');
echo '<ul>';
foreach ($folders as $folder) {
    echo '<li><a href="list-images.php?folder='.$folder.'">'.$folder.'</a></li>';
}
echo '</ul>';

The links created above have a dynamic variable created that will pass in the link to the list-images.php page.

Here is the list-images.php code:

if (isset($_GET['folder'])) {
    $folder = $_GET['folder'];
}
$singleImages = array();
foreach (glob($folder . '/*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $image) {
    $imageElements = array();
    $imageElements['source'] = $image;
    $singleImages[$image] = $imageElements;
}
echo '<ul>';
foreach ($singleImages as $image) {
    echo '<li><a href="'.$image['source'].'"><img src="'.$image['source'].'" width="400" height="auto"></a></li>';
}
echo '</ul>';

The links created here will link you to the individual images.