13
<?php 
$directory = '/var/www/ajaxform/';
if (glob($directory . '.jpg') != false)
{
    $filecount = count(glob($directory . '*.jpg'));
    echo $filecount;
}
else
{
    echo 0;
}
?>

there are four jpg images in this directory but it returns 0

Michel Feldheim
  • 17,625
  • 5
  • 60
  • 77
Rohit Goel
  • 3,396
  • 8
  • 56
  • 107

6 Answers6

30

Glob returns an array, on error it returns false.

Try this:

$directory = '/var/www/ajaxform/';
$files = glob($directory . '*.jpg');

if ( $files !== false )
{
    $filecount = count( $files );
    echo $filecount;
}
else
{
    echo 0;
}
Michel Feldheim
  • 17,625
  • 5
  • 60
  • 77
5

Try this:

<?php 
$directory = '/var/www/ajaxform/';
if (glob($directory . '*.jpg') != false)
{
 $filecount = count(glob($directory . '*.jpg'));
 echo $filecount;
}
else
{
 echo 0;
}
?>
karlingen
  • 13,800
  • 5
  • 43
  • 74
3

There is a mistake in your glob pattern (in the if). You are missing a *:

glob($directory . '*.jpg')

should work

kokx
  • 1,706
  • 13
  • 19
3

Minimalization approach:

function getImagesNo($path)
{
  return ($files=glob($path.'*.jpg')) ? count($files) : 0;
}
sbrbot
  • 6,169
  • 6
  • 43
  • 74
0

glob is case sensitive, according to the PHP docs. Are your extensions lowercase? Does the executing account have access to /var/www/ajaxform/?

jimf
  • 4,527
  • 1
  • 16
  • 21
0

Just try this--

if (glob($directory . "*.jpg") != false)
$filecount = count(glob($directory . "*.jpg"));
else
$filecount = 0;
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90