Home > Backend Development > PHP Tutorial > 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
Release: 2016-07-25 09:06:17
Original
854 people have browsed it
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


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template