[eluser]Yorick Peterse[/eluser]
This is a function I used for a simple image gallery, it's quite rough but it does what you are asking:
Code:
//Function to show a single category
function Category() {
$URL_Title = $_GET['title'];
if($URL_Title !== null) {
//Check if the category folders that belong to the current title exist and fetch their contents
$Full_Dir = "gallery/normal/$URL_Title/";
$Thumb_Dir = "gallery/thumbs/$URL_Title/";
//Scan directories
if(file_exists($Full_Dir)) {
$Scan_Full = scandir($Full_Dir);
}
if(file_exists($Thumb_Dir)) {
$Scan_Thumb = scandir($Thumb_Dir);
}
echo "<div id=\"image-wrap\">";
for($i = 0; $i<count($Scan_Thumb); $i++) {
if($Scan_Thumb[$i] != "." && $Scan_Thumb[$i] != ".." && preg_match('/[.](jpg)|(gif)|(png)$/',$Scan_Thumb[$i])) {
$FullName = str_replace("thumb_","",$Scan_Thumb[$i]);
$ThumbLayout = <<<THUMBS
<div class="image-box">
<a href="$Full_Dir$FullName" title="$FullName" rel="lightbox[roadtrip]"><img src="$Thumb_Dir$Scan_Thumb[$i]" alt="$Scan_Thumb[$i]" /></a>
</div>
THUMBS;
echo $ThumbLayout;
}
}
echo "</div>";
}
else {
echo "<h2>The page you requested does not exist!</h2>";
}
echo "</div>";
}
The most important part is the following:
Code:
if($Scan_Thumb[$i] != "." && $Scan_Thumb[$i] != ".." && preg_match('/[.](jpg)|(gif)|(png)$/',$Scan_Thumb[$i])) {
That piece of code ignores any folders that might have been placed in the folder you wish to scan, it will also ignore everything that does
not end with .jpg, .gif or .png
p.s. For all the MVC die-hards, I wrote this function before I learned about seperating logic from presentation