View Full Version : Dynamically build table of images from folders?
steamngn
05-17-2007, 09:00 PM
Here's a good one....
I have on our company web site a folder named "gallery". In that folder are MANY folders with varying names ("Bethel","Hamptonburgh",etc..). In each of these folders are a number of images and slideshow.php. What I am looking to do is dynamically build a Master page that will look in the Gallery folder, add a table cell for each sub folder, select 1 image from that folder (doesn't matter which one) to display in the cell, and give the cell a link to the corresponding slideshow.php for that group of images. The dynamic table needs to be 2 columns by "x" rows.
Now, I can build a table using php, but how the heck do I go about getting all the folder links and images? Any ideas? The sub folders change constantly, with new stuff added and old stuff removed. If this can be done it would save me a TON of edit work on just this one page alone...
Andy
Fumigator
05-17-2007, 09:26 PM
It's not only possible, it's easy. Look into glob() (http://us.php.net/manual/en/function.glob.php) (specifically using the GLOB_ONLYDIR directive).
steamngn
05-18-2007, 03:37 PM
Hey Fumigator...
After farting around with GLOB and searching the web, I came across this:
<?php
//get file listing from the current and sub directories and show them as anchored
//(linked) files.
$filelist = explode("\n",`find .|sort`);
// for each item (file) in the array...
for ($count=0;$count<count($filelist);$count++) {
// get the filename
$filename=$filelist[$count];
// if it's not a directory, display linked
if (!is_dir($filename) && (eregi("(\.jpg|\.gif|\.png)$", $filename)))
printf("<a href=\"%s\">%s</a><br>\n",$filename,$filename);
// otherwise if it is a directory
else if (is_dir($filename))
printf("<p>Category: %s<p>\n",$filename);
}
?>
Which parses out a list of the directories nicely. I was able to add the "eregi" part (through trial and error) to get it to parse out only images, but I can't figure out a few things:
1) how do I get it to return only one image file from each directory?
2) if there are subdirectories in the subdirectories, and we want to ignore them, how do we do that??
Otherwise, I think this will work pretty well (Unless of course you want to educate my dumb @$$ about something better!:D )
Andy
steamngn
05-19-2007, 04:00 PM
Ok,
The 99% solution. This is working pretty well, but there seems to be some issues with it iterating through more than one subdirectory level:
<?php
//get file listing from the current and sub directories and show them as anchored
//(linked) files.
$filelist = explode("\n",`find .|sort`);
$c = 0;
$d = 0;
// for each item (file) in the array...
for ($count=0;$count<count($filelist);$count++) {
// get the filename
$filename=$filelist[$count];
// if it's not a directory, display linked
if (!is_dir($filename) && (eregi("(\.jpg|\.gif|\.png)$", $filename)) && (!eregi("_vti_cnf",$filename))){
if ($c == 0){
echo ("$filename $c <br>\n");
$c = $c + 1;
}
if (eregi("(\.jpg|\.gif|\.png)$", $filename)){
$d = $d + 1;
}
}
// otherwise if it is a directory
else if (is_dir($filename) && (!eregi("_vti_cnf",$filename)) && ($filename!=".") && ($filename!="..") && ($d!=0)){
echo (" There are ". max($d,0) ." images in this directory <br>\n");
echo ("<p>Directory: $filename<br>\n");
$d = 0;
$c = 0;
}
}
?>
Also of interest is the fact that if I leave out the check for $d!=0 there is still one "ghost" directory that this will return. I can't seem to find it, but i am assuming there is something on the server I am not seeing...
Andy
steamngn
05-21-2007, 01:37 AM
HELP!
so close....
<?php
$filelist = explode("\n",`find .|sort`);
$dircount = 1;
$imgcount = 0;
$display_block = "<table style=\"width:100%\">";
$display_block .= "<tr> \n";
//for each item in the array....
for ($count=0;$count<count($filelist);$count++) {
// get the file names....
$filename = $filelist[$count];
// get the directories....
if (is_dir($filename) && (!eregi("_vti_cnf",$filename)) && ($filename!=".") && ($filename!="..")) {
$currdir = array();
$gallery = opendir($filename);
$imgcount = 0;
//loop to count number of images....
while($f = readdir($gallery)){
if(($f != ".") && ($f != "..")&& (eregi("(\.jpg|\.gif|\.png)$", $f))){
//get an image to display....
array_push($currdir,"$f");
$d = (array_rand($currdir));
$displayimg = sprintf($d,s);
$imgcount++;
}
}
closedir($handle);
//build our table...
$display_block .= "<td><a href=\"slideshow.php?dir=" . $filename . "\">" . $dircount . "[" . $displayimg . "]" . $imgcount . "</a></td>\n";
$dircount = $dircount + 1;
if ($dircount % 2) {
$display_block .= "</tr>\n<tr>\n";
} else {
$display_block .= "";
}
$imgid = 1;
}
}
//close our table when done...
$display_block .="</tr></table>";
//show our work!
echo $display_block;
?>
This returns all directories with images fine, but I cannot get the random file name out of the loop to display in $displayimg for the generated table. All I get is a numeral (The array key?) instead of the filename/value.
Waht am I missing???
Andy
PappaJohn
05-21-2007, 02:13 AM
array_rand() returns the key.
Instead of this:
$d = (array_rand($currdir));
try
$d = $currdir[(array_rand($currdir))];
steamngn
05-21-2007, 02:31 PM
PappaJohn!
Right on the money. I will post the entire code for this gallery slideshow as soon as it is done; VERY simple to implement! I knew I had my head stuck in my *** about getting out the string....
Reps all around!
Andy
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.