akinuri
akinuri

Reputation: 12027

Get file and folder count

In my lyrics application, I'm using a multi-dimensional array to print the artists and album/track counts.

$test_arr = [
    /* artist               albums  tracks */
    [ "Green Day",          "8",    "26",   ],
    [ "Remy Zero",          "1",    "2",    ],
    [ "System of a Down",   "1",    "1",    ],
    [ "Modern Talking",     "1",    "1",    ],
    [ "Snow Patrol",        "1",    "2",    ],
    [ "Linkin Park",        "6",    "18",   ],
    [ "3 Doors Down",       "5",    "13",   ],
    /* ... */
];

In the array, I enter the album and track counts manually. I don't want to do that. Is there a way to add them with PHP?

Take 3 Doors Down for example. If I go to folder properties of 3 Doors Down, there I can see:

Contains:   13 Files, 5 Folders

and the folder tree of 3 Doors Down

...\3_doors_down
+---2000_the_better_life
|       01_kryptonite.txt
|       03_duck_and_run.txt
|       05_be_like_that.txt
|       11_so_i_need_you.txt
|       
+---2002_away_from_the_sun
|       06_here_without_you.txt
|       09_changes.txt
|       
+---2005_seventeen_days
|       03_let_me_go.txt
|       07_behind_those_eyes.txt
|       12_here_by_me.txt
|       
+---2008_3_doors_down
|       03_its_not_my_time.txt
|       05_pages.txt
|       
\---2011_time_of_my_life
        08_whats_left.txt
        12_believer.txt

Is there a way to count the total folders and files in a given directory?

Upvotes: 0

Views: 59

Answers (1)

Kohjah Breese
Kohjah Breese

Reputation: 4136

To count all files and folders in a directory:

count( glob( 'directory/*' ) );

To just count directories:

$i = 0;
$ps = glob( 'directory/*' );
foreach( $ps as $p )
{
    if( is_dir( $p ) )
        $i++;
}

Upvotes: 1

Related Questions