note 113903 deleted from function.scandir by cmb
| From: | cmb@php.net | Date: | Sun, 17 Nov 2019 09:06:21 +0000 |
| Subject: | note 113903 deleted from function.scandir by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213122@lists.php.net to get a copy of this message | ||
Note Submitter: donovan dot pp at gmail dot com
----
Non-recursive way (red: FASTER & less memory consumption) to list all directory content :
<?php
function list_directory($dir) {
$file_list = array();
$stack[] = $dir;
while ($stack) {
$current_dir = array_pop($stack);
if ($dh = opendir($current_dir)) {
while (($file = readdir($dh)) !== false) {
if ($file !== '.' AND $file !== '..') {
$current_file = "{$current_dir}/{$file}";
$report = array();
if (is_file($current_file)) {
$file_list[] = "{$current_dir}/{$file}";
} elseif (is_dir($current_file)) {
$stack[] = $current_file;
$file_list[] = "{$current_dir}/{$file}/";
}
}
}
}
}
return $file_list;
}
// usage :
//print_r(list_directory('C:/'));
//print_r(list_directory('C:/Users'));
print_r(list_directory('/var/www'));
?>