I want to load images dynamically from a folder. Since I don't know how many images are in the folder I was wondering if there was a way to check that folder for the number of files so that I could then write a for loop to display them.
I want to load images dynamically from a folder. Since I don't know how many images are in the folder I was wondering if there was a way to check that folder for the number of files so that I could then write a for loop to display them.
You can grab all the jpg images in an array by calling:
Then if you still needed to count them:PHP Code:$images = glob('./dir/*.jpg');
PHP Code:$imgcount = count($images);
List all of the images in a folder:
I also use a script from this site for a gallery http://www.dynamicdrive.com/dynamici...photoalbum.htmPHP Code:<?php
$imgdir = 'images'; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif','bmp'); // list of filetypes you want to show
$dimg = opendir($imgdir);
while($imgfile = readdir($dimg))
{
if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
{
$a_img[] = $imgfile;
sort($a_img);
reset ($a_img);
}
}
$totimg = count($a_img); // total image number
for($x=0; $x < $totimg; $x++)
{
$size = getimagesize($imgdir.'/'.$a_img[$x]);
// do whatever
$halfwidth = ceil($size[0]/2);
$halfheight = ceil($size[1]/2);
echo 'name: '.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br />';
}
?>
Last edited by james438; 09-17-2008 at 12:48 AM.
so then if I want to display them I would do it like this?
would this work...sorry I cannot test it at this time so I'm just trying to figure if that would work when I get home later...Code:<?php $images = glob('./dir/*.jpg'); for($i = 0; $i < $images-1; $i++) { echo "<img src='" . $images[$i] . "' />"; ?>
@james
That seems like a lot of fluff to do such an easy task. (you also have some errors in it)
@mosh
PHP Code:<?php
$images = glob('./dir/*.jpg');
foreach($images as $img)
echo "<img src='" . $img . "' />";
?>
Moshambi (09-16-2008)
true, I have not completely debugged it yet, but why would you want to only display jpeg files?
here is a more watered down version if you prefer, with the bugs removed. I wasn't able to figure out how to sort it in a case insensitive way yet though.
PHP Code:<?php
$imgdir = 'images';
$allowed_types = array('png','jpg','peg','gif','bmp');
$dimg = opendir($imgdir);
while($imgfile = readdir($dimg))
{
if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
{
$a_img[] = $imgfile;
}
}
sort($a_img);
$totimg = count($a_img);
for($x=0; $x < $totimg;)
{
echo "$a_img[$x]<br>";
$x++;
}
?>
Last edited by james438; 09-15-2008 at 10:28 PM.
Thank you so much techietim that worked wonders!
[Spam removed.]
Last edited by djr33; 09-17-2008 at 06:47 PM.
Bookmarks