Home  >  Article  >  Backend Development  >  Traverse the file directory and return all file names in the directory using the path as the key value.

Traverse the file directory and return all file names in the directory using the path as the key value.

WBOY
WBOYOriginal
2016-07-25 09:06:17845browse
Array
(
[C:/testnice] => Array
([0] => New text document.txt)

[C:/testnice copy nice] => Array
( )

[C:/testnice copy nice copy nice] => Array
([0] => New text document.txt)

[C:/test copy (2) nice] => Array
([0] => New text document.txt)
)
  1. function read_dir_test(& $fileArr, $dir) {
  2. if (!$dir) {
  3. return null;
  4. }
  5. $dirHandle = opendir($dir);
  6. while ($entry = readdir($dirHandle) ) {
  7. if ($entry != '.' && $entry != '..') {
  8. if (is_dir($dir . DIRECTORY_SEPARATOR . $entry)) {
  9. $fileArr[$dir . DIRECTORY_SEPARATOR . $entry] = array();
  10. $fileArr = array_merge($fileArr, read_dir_test($fileArr[$dir . DIRECTORY_SEPARATOR . $entry], $dir . DIRECTORY_SEPARATOR . $entry));
  11. } else {
  12. $fileArr[$dir][ ] = $entry;
  13. }
  14. }
  15. }
  16. return $fileArr;
  17. }
  18. $fileArr = array();
  19. $dir = "C:/test";
  20. read_dir_test($fileArr, $dir);
  21. print_r ($fileArr);
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn