获取文件和文件夹计数

时间:2014-08-25 19:35:35

标签: php file-io

在我的歌词应用程序中,我使用多维数组来打印艺术家和专辑/曲目计数。

$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",   ],
    /* ... */
];

在数组中,我手动输入相册和曲目计数。我不想那样做。有没有办法用PHP添加它们?

以3门为例。如果我转到3 Doors Down的文件夹属性,我可以看到:

Contains:   13 Files, 5 Folders

和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

有没有办法计算给定目录中的总文件夹和文件?

1 个答案:

答案 0 :(得分:1)

计算目录中的所有文件和文件夹:

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

只计算目录:

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