With a few lines of code, we can output all the images within a folder. Changing this simple code a bit, we could also list text files, zips... whatever you have on your folder, but we will be doing it now with images, so whatever folder you use, it should be filled with images only, the extension doesn't matter. Please see the number next to each line of code for more information below.
<? $folder = "path_to/your_folder"; //1
if ($handle = opendir('$folder')) { //2
while (false !== ($file = readdir($handle))) { //3
if ($file != "." && $file != "..") //4
echo "<img src=\"$folder/$file\" alt=\"\" />"; //5
}
} ?>
Explanation on each line:
Basically what it does is open the folder and read all the contents within it and output each one with a loop until there aren't any other files. It's all commented above.
To use it with other kinds of documents you just need to change the echo to, for example an url, like this:
<?php echo "<a href=\"$folder/$file\">$file</a>"; ?>
Tutorial © Melfina. Do not reproduce in any way without previous permission from the author. Printed from http://celestial-star.net