IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

php 返回目录下的所有文件名/文件夹类

Everyday NetLog 2010-03-24 23:37:49 累计浏览 4,452 次
本机暂存
PHP读取目录树下的文件及目录方法。
这里为了便于演示,直接列出来了,其中可以返回数组之类的,根据需要另作改动。
代码如下,可以直接Copy测试。
 
  1. <?php   
  2.  main();   
  3.   
  4.  function main(){   
  5.  //Set Variables   
  6.  $testDirName = "./zhoz/";   
  7.  $fileListArray = array();   
  8.  $dirListArray = array();   
  9.   
  10. $aDirectory = new DirReader($testDirName);   
  11.   
  12. // Store File List in Array   
  13. $fileListArray = $aDirectory->getFileList();   
  14. $dirListArray = $aDirectory->getDirList();   
  15.   
  16. echo "<html><body>\n";   
  17. echo "<pre>\n";   
  18.   
  19.   
  20. echo "Reading Directory: ".  $aDirectory->getDirPath() ."\n";   
  21. echo "Current Directory: "getcwd() ."\n";   
  22.   
  23. echo "-- Files in Directory --\n";   
  24. foreach ($fileListArray as $filename) {   
  25.     echo "File: $filename\n";   
  26. }   
  27.   
  28. echo "\n-- Sub Directories --\n";   
  29. foreach ($dirListArray as $filename){   
  30.     echo "Sub dir: $filename\n";   
  31. }   
  32. echo "</pre></body></html>\n";   
  33. }   
  34.   
  35. class DirReader{   
  36.   private $dh;   
  37.   private $basedir;   
  38.   private $fileNameArray=array();   
  39.   private $dirNameArray=array();   
  40.   
  41.   function __construct($dirname){   
  42.     $this->basedir = $dirname;   
  43.     $this->dh = dir($dirnameor die($php_errormsg);   
  44.     $this->parseDirectory();   
  45.   }   
  46.   
  47.   function parseDirectory(){   
  48.     $filename = "";   
  49.     while (false !== ($filename = $this->dh->read())){   
  50.       $fullpath = $this->basedir . '/' . $filename;   
  51.       if (is_file($fullpath)){   
  52.         array_push($this->fileNameArray, $filename);   
  53.       }else{   
  54.         array_push($this->dirNameArray, $filename);   
  55.       }   
  56.     }   
  57.   }   
  58.   
  59.   
  60.   // Get all the files in the directory   
  61.   function getFileList(){   
  62.     return $this->fileNameArray;   
  63.   }   
  64.   
  65.   // Get all the sub directories in the directory   
  66.   function getDirList(){   
  67.     return $this->dirNameArray;   
  68.   }   
  69.   
  70.   function getDirPath(){   
  71.     return $this->dh->path;   
  72.   }   
  73. }   

同分类推荐文章

  1. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

  1. 使用gettext来支持PHP的多语言 (累计阅读 39,270)
  2. WordPress插件开发 -- 在插件使用数据库存储数据 (累计阅读 29,164)
  3. Paypal接口详细代码(PHP版,非API接口) (累计阅读 19,408)
  4. 我的PHP,Python和Ruby之路 (累计阅读 13,150)
  5. include(“./file.php”)和include(“file.php”)区别 (累计阅读 12,791)
  6. 15个最好的免费开源电子商务平台 (累计阅读 12,541)
  7. Redis消息队列的若干实现方式 (累计阅读 12,088)
  8. 到底什么是MVC? (累计阅读 11,869)
  9. 整理了一份招PHP高级工程师的面试题 (累计阅读 11,709)
  10. Rolling cURL: PHP并发最佳实践 (累计阅读 11,488)