Viewed   121 times

I'm working on a slightly new project. I wanted to know how many files are in a certain directory.

<div id="header">
    $dir = opendir('uploads/'); # This is the directory it will count from
    $i = 0; # Integer starts at 0 before counting

    # While false is not equal to the filedirectory
    while (false !== ($file = readdir($dir))) { 
        if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;

    echo "There were $i files"; # Prints out how many were in the directory

This is what I have so far (from searching). However, it is not appearing properly? I have added a few notes so feel free to remove them, they are just so I can understand it as best as I can.

If you require some more information or feel as if I haven't described this enough please feel free to state so.



You can simply do the following :

$fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
printf("There were %d Files", iterator_count($fi));
Saturday, September 10, 2022

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.)

Friday, December 16, 2022


$dir = opendir('dir/');
$i = 0;
while (false !== ($file = readdir($dir))){
    if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;

echo "There were $i files";


function crawl($dir){

    $dir = opendir($dir);
    $i = 0;
    while (false !== ($file = readdir($dir)){
            if (is_dir($file) and !in_array($file, array('.', '..'))){

            $i += crawl($file);

    return $i;
$i = crawl('dir/');
echo "There were $i files";
Monday, August 15, 2022

For the fun of it I've whipped this together:

class FileFinder
    private $onFound;

    private function __construct($path, $onFound, $maxDepth)
        // onFound gets called at every file found
        $this->onFound = $onFound;
        // start iterating immediately
        $this->iterate($path, $maxDepth);

    private function iterate($path, $maxDepth)
        $d = opendir($path);
        while ($e = readdir($d)) {
            // skip the special folders
            if ($e == '.' || $e == '..') { continue; }
            $absPath = "$path/$e";
            if (is_dir($absPath)) {
                // check $maxDepth first before entering next recursion
                if ($maxDepth != 0) {
                    // reduce maximum depth for next iteration
                    $this->iterate($absPath, $maxDepth - 1);
            } else {
                // regular file found, call the found handler
                call_user_func_array($this->onFound, array($absPath));

    // helper function to instantiate one finder object
    // return value is not very important though, because all methods are private
    public static function find($path, $onFound, $maxDepth = 0)
        return new self($path, $onFound, $maxDepth);

// start finding files (maximum depth is one folder down) 
$count = $bytes = 0;
FileFinder::find('.', function($file) use (&$count, &$bytes) {
    // the closure updates count and bytes so far
    $bytes += filesize($file);
}, 1);

echo "Nr files: $count; bytes used: $bytesn";

You pass the base path, found handler and maximum directory depth (-1 to disable). The found handler is a function you define outside, it gets passed the path name relative from the path given in the find() function.

Hope it makes sense and helps you :)

Tuesday, November 15, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :