View Full Version : Checking number of files in a folder
Moshambi
09-15-2008, 06:49 PM
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.
techietim
09-15-2008, 07:17 PM
You can grab all the jpg images in an array by calling:
$images = glob('./dir/*.jpg');
Then if you still needed to count them:
$imgcount = count($images);
james438
09-15-2008, 08:20 PM
List all of the images in a folder:
<?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 />';
}
?>
I also use a script from this site for a gallery http://www.dynamicdrive.com/dynamicindex4/php-photoalbum.htm
Moshambi
09-15-2008, 08:56 PM
so then if I want to display them I would do it like this?
<?php
$images = glob('./dir/*.jpg');
for($i = 0; $i < $images-1; $i++)
{
echo "<img src='" . $images[$i] . "' />";
?>
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...
techietim
09-15-2008, 09:43 PM
@james
That seems like a lot of fluff to do such an easy task. (you also have some errors in it)
@mosh
<?php
$images = glob('./dir/*.jpg');
foreach($images as $img)
echo "<img src='" . $img . "' />";
?>
james438
09-15-2008, 09:55 PM
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
$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++;
}
?>
Moshambi
09-16-2008, 05:30 AM
Thank you so much techietim that worked wonders!
rjrenjian
09-17-2008, 06:38 AM
[Spam removed.]
techietim
09-17-2008, 10:13 AM
Nurse: How do you feel after your operation? Patient: Quite alright, only I can feel two hearts beating inside me. Nurse: No wonder the doctor who operated on you was looking for his replica rolex everywhere just now.
Okay, I must say that was the funnest spam post I've ever seen. :D
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.