1:<?php 2:main(); 3: 4:function main(){ 5: //Set Variables 6: $testDirName = "test_dir"; 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; # Directory Handle 37: private $basedir; # Base Directory passed to the object 38: private $fileNameArray=array(); 39: private $dirNameArray=array(); 40: 41: function __construct($dirname){ 42: $this->basedir = $dirname; 43: $this->dh = dir($dirname) or 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:} 74:?>