Home >Backend Development >PHP Tutorial >PHP iterate through directory and file lists

PHP iterate through directory and file lists

WBOY
WBOYOriginal
2016-07-25 08:43:151082browse
  1. define('DS', DIRECTORY_SEPARATOR);
  2. class getDirFile{
  3. //返回数组
  4. private $DirArray = array();
  5. private $FileArray = array();
  6. private $DirFileArray = array();
  7. private $Handle,$Dir,$File;
  8. //获取目录列表
  9. public function getDir( & $Dir ){
  10. if( is_dir($Dir) ){
  11. if( false != ($Handle = opendir($Dir)) ){
  12. while( false != ($File = readdir($Handle)) ){
  13. if( $File!='.' && $File!='..' && !strpos($File,'.') ){
  14. $DirArray[] = $File;
  15. }
  16. }
  17. closedir( $Handle );
  18. }
  19. }else{
  20. $DirArray[] = '[Path]:''.$Dir.'' is not a dir or not found!';
  21. }
  22. return $DirArray;
  23. }
  24. //获取文件列表
  25. public function getFile( & $Dir ){
  26. if( is_dir($Dir) ){
  27. if( false != ($Handle = opendir($Dir)) ) {
  28. while( false != ($File = readdir($Handle)) ){
  29. if( $File!='.' && $File!='..' && strpos($File,'.') ){
  30. $FileArray[] = $File;
  31. }
  32. }
  33. closedir( $Handle );
  34. }
  35. }else{
  36. $FileArray[] = '[Path]:''.$Dir.'' is not a dir or not found!';
  37. }
  38. return $FileArray;
  39. }
  40. //获取目录/文件列表
  41. public function getDirFile( & $Dir ){
  42. if( is_dir($Dir) ){
  43. $DirFileArray['DirList'] = $this->getDir( $Dir );
  44. if( $DirFileArray ){
  45. foreach( $DirFileArray['DirList'] as $Handle ){
  46. $File = $Dir.DS.$Handle;
  47. $DirFileArray['FileList'][$Handle] = $this->getFile( $File );
  48. }
  49. }
  50. }else{
  51. $DirFileArray[] = '[Path]:''.$Dir.'' is not a dir or not found!';
  52. }
  53. return $DirFileArray;
  54. }
  55. }
  56. ?>
复制代码

实例:(相对路径或绝对路径)

1.获取目录列表
  1. $Dir_dir = './example';
  2. $getDirFile = new getDirFile();
  3. $getDir = $getDirFile->getDir( $Dir_dir );
  4. print_r($getDir);
  5. ?>
  6. 显示:
  7. [html] view plaincopy
  8. Array
  9. (
  10. [0] => example_one
  11. [1] => example_two
  12. )
复制代码

2.获取文件列表
  1. $File_one_dir = './example/example_one';
  2. $File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two';
  3. $getDirFile = new getDirFile();
  4. $getFile_one = $getDirFile->getFile( $File_one_dir );
  5. $getFile_two = $getDirFile->getFile( $File_two_dir );
  6. print_r($getFile_one);
  7. print_r($getFile_two);
  8. ?>
复制代码

显示:
  1. Array
  2. (
  3. [0] => example.sql
  4. [1] => example.txt
  5. )
  6. Array
  7. (
  8. [0] => example.php
  9. )
复制代码

3.获取目录/文件列表
  1. $Dir_dir = './example';
  2. $getDirFile = new getDirFile();
  3. $getDirFile = $getDirFile->getDirFile( $Dir_dir );
  4. print_r($getDirFile);
  5. ?>
复制代码

显示:
  1. Array
  2. (
  3. [DirList] => Array
  4. (
  5. [0] => example_one
  6. [1] => example_two
  7. )
  8. [FileList] => Array
  9. (
  10. [example_one] => Array
  11. (
  12. [0] => example.sql
  13. [1] => example.txt
  14. )
  15. [example_two] => Array
  16. (
  17. [0] => example.php
  18. )
  19. )
  20. )
复制代码

File list, PHP


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
Previous article:php post json dataNext article:php post json data