0

im looking a better way to count how many images are in a folder, im using this script:

$direccion = "/var/www/XXXX/XXX/XXX/httpdocs/images/helo/";

if (glob($direccion . "*.jpg") != false)
{
 $filecount1 = count(glob($direccion . "*.jpg"));
}
else
{
 $filecount1 = 0;
}

if (glob($direccion . "*.jpeg") != false)
{
 $filecount2 = count(glob($direccion . "*.jpeg"));
}
else
{
 $filecount2 = 0;
}

if (glob($direccion . "*.JPG") != false)
{
 $filecount3 = count(glob($direccion . "*.JPG"));
}
else
{
 $filecount3 = 0;
}

if (glob($direccion . "*.JPEG") != false)
{
 $filecount4 = count(glob($direccion . "*.JPEG"));
}
else
{
 $filecount4 = 0;
}

$conteodefotos = $filecount4 + $filecount3 + $filecount2 + $filecount1;

echo $conteodefotos;

But this will not count if the extension has mix capital letters, like "file.JpG"

Is there a simple and more eficient way to do this?

  • It's an old link but this seems to answer the question. http://stackoverflow.com/questions/2520612/can-phps-glob-be-made-to-find-files-in-a-case-insensitive-manner – Joel Aug 22 '13 at 02:55

1 Answers1

6
$images = glob($direccion . "*.[jJ][pP]{[eE],}[gG]", GLOB_BRACE);

echo $images ? count($images) : 0;
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292