0

I have folder structure like below in my web application.

enter image description here

How can I get the number of files in each folder recursively using php. I'm working with Codeignaiter. I haven't any idea for how to do this task.

I can't use scandir function because there are no physical directory in path.Only files and folders save in database.

This is my database

enter image description here

Please any help needed. Thank you.

3 Answers3

0

Try this function by passing the path as a parameter in it:

function getFileCount($path) {
        $size = 0;
        $ignore = array('.','..','cgi-bin','.DS_Store');
        $files = scandir($path);
        foreach($files as $t) {
            if(in_array($t, $ignore)) continue;
            if (is_dir(rtrim($path, '/') . '/' . $t)) {
                $size += getFileCount(rtrim($path, '/') . '/' . $t);
            } else {
                $size++;
            }   
        }
        return $size;
    }
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
  • Thank you for your effort.but I can't use this because its not physical directory.and I can't use `scandir` function because there is no any directory path.only save in database. – Menaka Kariyawasam Nov 23 '16 at 05:50
0

If you would like to get, say, all the *.php files in your project folder, recursively, you could use the following: Source

<?php

$Directory = new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);

?>

$Regex will contain a single index array for each PHP file.

TIGER
  • 2,864
  • 5
  • 35
  • 45
0

You can extend RecursiveArrayIterator to get kind of RecursiveDirectoryIterator but for your virtual filesystem:

class RecursiveVirtualDirectoryIterator extends RecursiveArrayIterator
{
    private $files;

    public function __construct($parentId, $array = [], $flags = 0)
    {
        $this->files = $array;

        parent::__construct(
            $this->getFilesByParentId($parentId),
            $flags
        );
    }

    private $children;
    public function hasChildren()
    {
        $file = $this->current();

        if ($file['is_file']) {
            return false;
        }

        $this->children = $this->getFilesByParentId($file['id']);

        return !empty($this->children);
    }

    private function getFilesByParentId($id)
    {
        return array_filter($this->files, function ($file) use ($id) {
            return $file['parent_id'] === $id;
        });
    }

    public function getChildren()
    {
        $file = $this->current();
        return new static(
            $file['id'],
            $this->children,
            $this->getFlags()
        );
    }
}

Then you can iterate over your array returned from databases using RecursiveIteratorIterator and count files for, say, topmost folders:

$iterator = new RecursiveIteratorIterator(
    new RecursiveVirtualDirectoryIterator(0, $files),
    RecursiveIteratorIterator::SELF_FIRST
);

$currentDirectoryName = null;
$filesCount = [];
foreach ($iterator as $file) {
    if ($iterator->getDepth() === 0 && !$file['is_file']) {
        $currentDirectoryName = $file['name'];
        $filesCount[$currentDirectoryName] = 0;
        continue;
    }

    $filesCount[$currentDirectoryName] += 1; 
}

Here is working demo.

While Standard PHP Library (SPL) is poorly documented, it contains many useful things that save you from reinventing the wheel over and over again.

sevavietl
  • 3,762
  • 1
  • 14
  • 21