<p>Is there a better/simpler way to find the number of images in a directory and output them to a variable?</p>

<pre><code>function dirCount($dir) {
  $x = 0;
  while (($file = readdir($dir)) !== false) {
    if (isImage($file)) {$x = $x + 1}
  }
  return $x;
}
</code></pre>

<p>This seems like such a long way of doing this, is there no simpler way?</p>

<p><strong>Note:</strong> The isImage() function returns true if the file is an image.</p>

Check out the Standard PHP Library (aka SPL) for DirectoryIterator:
```
```
$dir = new DirectoryIterator('/path/to/dir');
foreach($dir as $file ){
  $x += (isImage($file)) ? 1 : 0;
}
```
```
(FYI there is an undocumented function called iterator_count() but probably best not to rely on it for now I would imagine. And you'd need to filter out unseen stuff like . and .. anyway.)