-2

I have a folder with lots of subfolders and a large number of pdf files inside them...Now i want to list the folder names with the number of pdf files inside each folder using PHP.How to accomplish this?

1 Answers1

1

Using PHP recursive function you can get simple array for folders.

PHP

<?php
    $array=array();
    function getCount($path){
        $count=0;
        global $array;
        //$count=count(glob($path."/*"));
        foreach(new DirectoryIterator($path) as $fileInfo){
            if($fileInfo->isDir()){
                if($fileInfo->isDot()) continue;
                if(array_key_exists($fileInfo->getFilename(),$array)){
                    $array[$fileInfo->getFilename()] = array(
                        'is_file_directory' => 'file',
                        'count' => getCount($fileInfo->getPathname())
                    );
                }
                else{
                    $array[$fileInfo->getFilename()] = array(
                        'is_file_directory' => 'directory',
                        'count' => getCount($fileInfo->getPathname())
                    );
                }
            }
            else{
                $array[$fileInfo->getFilename()] = array(
                    'is_file_directory' => 'file',
                    'count' => 0
                );
                $count++;
            }
        }
        return $count;
    }
?>

How to use : getCount('folder/inner_folder'); print_r($array);

Jaydeep Mor
  • 1,690
  • 3
  • 21
  • 39