Page 1 of 1

image count in a directory

Posted: Sat Feb 04, 2006 5:38 pm
by jasondavis
I need to show the count of imnages in a specific directory, I have this code below but it only shows
.JPG

I need to add ability to count images with
.jpg
.PNG
.png
.gif
.GIF

any help appreciated

Code: Select all

<?php
$my_jpg_counter=0;
$d = dir($my_dir = "../signs/tobeapproved");
while (false !== ($entry = $d->read())) {
    $ext = strrchr($entry, ".");
    if($ext == ".JPG")
        $my_jpg_counter++;
}
$d->close();

echo "<font color=\"0270CA\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"3\"><strong>(<font color=\"000000\">$my_jpg_counter</font>)</strong></font>";
?>

Posted: Sat Feb 04, 2006 6:01 pm
by John Cartwright
You could try

Code: Select all

$validExtensions = array('jpg' => 0, 'gif' => 0, 'png' => 0);

foreach (glob($dir.'{*.'. implode(', *.', array_keys($validExtensions)) .'}', GLOB_BRACE) as $fileName) {
	$file = explode('.', $fileName);
	$file = array_reverse($file);
	$validExtensions[$file[0]]++;
}

print_r($validExtensions);
I acutally just wrote nearly the exact same thing :). Enjoy.

Posted: Sat Feb 04, 2006 6:44 pm
by jasondavis
sorry im new how wuld I put that into use?

Posted: Sat Feb 04, 2006 8:37 pm
by m3mn0n
A file named whatever.jpeg won't be recognized.

Posted: Sat Feb 04, 2006 8:54 pm
by feyd
why not use getimagesize() if you're looking for images? At least then, you can verify they are as close as possible to being images as possible...