/ Published in: PHP
Sometime it is convenient to get recursive directory listing as array of items
directory => (file array)
Such as:
Array
(
[./a2temple] => Array
(
[0] => example13.zip
)
[./rand] => Array
(
[0] => rand.zip
)
[./pstudio] => Array
(
[0] => pstudio.zip
)
[./myview/private] => Array
(
[0] => logs.zip
)
[./myview] => Array
(
[0] => myview-2.zip
)
[.] => Array
(
[0] => testutf.zip
[1] => RBAC.ZIP
)
)
My function can get optional extension filter and returns count of files found.
directory => (file array)
Such as:
Array
(
[./a2temple] => Array
(
[0] => example13.zip
)
[./rand] => Array
(
[0] => rand.zip
)
[./pstudio] => Array
(
[0] => pstudio.zip
)
[./myview/private] => Array
(
[0] => logs.zip
)
[./myview] => Array
(
[0] => myview-2.zip
)
[.] => Array
(
[0] => testutf.zip
[1] => RBAC.ZIP
)
)
My function can get optional extension filter and returns count of files found.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php function fillEntries($dir, &$entries, $ext = '') { while (($entry = $d->read()) !== false) { // skip hidden files, . and .. if ($entry{0} != '.') { { // case insensitive extension test $subEntries[] = $entry; } else fillEntries($dir.'/'.$entry, $entries, $ext); } } $d->close(); $entries[$dir] =& $subEntries; // return count of "leaf" nodes } // NB: extension parameter with dot char $count = fillEntries('.', $entries, '.zip'); echo "There are {$count} entries: <pre> {$content} </pre> ";