找到之前的一篇筆記,關於PHP遍歷目錄類。發佈出來,加深印象
1: /**
2: * Open Read Write Close Dir and File
3: *
4: * @author Yaron [[email protected],http://yaron.org.cn]
5: * @version 0.1
6: * @package
7: */
8:
9: class fileDirOpt {
10: var $dirPath;
11: function openDir($dirPath){
12: $this->dirPath = $dirPath;
13: if (is_dir($dirPath)){
14: $dir = opendir($dirPath);
15: return $dir;
16: }else{
17: die("$dirPath is Not a Directory");
18: }
19: }
20: function closeDir($dir) {
21: closedir($dir);
22: }
23: function listDir($dir){
24: echo '
- '
25: while($file = readdir($dir)){
26: if($file!='.' && $file!='..'){ // filter . and ..
27: $dd = $this->dirPath; //
28: $dd = $dd.'/'.$file;
29: echo "$file";
30: }
31: if(is_dir($dd) && $file!='.' && $file!='..') { // is_dir 參數需要完整的路徑
32: $subDir = $this->openDir($dd);
33: $this->listDir($subDir);
34: $this->closeDir($subDir);
35: }
36: }
37: echo '';
38: return true;
39: }
40: }
41:
42: $dirOpt = new fileDirOpt();
43: $dirOpt->dirPath = 'd:/xampp';
44: $dir = $dirOpt->openDir($dirOpt->dirPath);
45: $dirOpt->listDir($dir);
46: $dirOpt->closeDir($dir);
一位牛人說,優秀的代碼不需要註釋!言外之意是說不用註釋,閱讀者就能很好的理解!筆者沒有達到這個水平,還是稍稍註釋,其實用到的都是最基本的目錄函數。