<?phperror_reporting(E_ALL);ini_set('display_errors','on');require_once'Zend/Loader/Autoloader.php';Zend_Loader_Autoloader::getInstance();classDirectoryTraverser{constFILE='file';constDIR='directory';// 当前目录 protected$_directory;// 深度 protected$_depth;publicfunction__construct(DirectoryIterator$directory,$depth=0){if(!($directoryinstanceofDirectoryIterator)){thrownewException("Need a DirectoryIterator instance!");}$this->_directory=$directory;$this->_depth=$depth;}publicfunctiondump(){foreach($this->_directoryas$fileInfo){// 隐藏目录和上级目录不处理 if($fileInfo->isDot()||substr($fileInfo->getFilename(),0,1)=='.'){continue;}if($fileInfo->isFile()){// 如果是文件,直接打印文件名 $this->dumpFile($fileInfo);}elseif($fileInfo->isDir()){// 如果是目录,打印目录名后深入 $this->dumpDirectory($fileInfo);}}}protectedfunction_dump(DirectoryIterator$fileInfo,$type){if(!($fileInfoinstanceofDirectoryIterator)){thrownewException("Need a DirectoryIterator instance!");}Zend_Debug::dump(str_repeat(" ",$this->_depth*4)."This is a {$type}: ".$fileInfo->getPathname());}publicfunctiondumpFile(DirectoryIterator$fileInfo){$this->_dump($fileInfo,self::FILE);}publicfunctiondumpDirectory(DirectoryIterator$fileInfo){$this->_dump($fileInfo,self::DIR);$dt=newDirectoryTraverser(newDirectoryIterator($fileInfo->getPathname()),$this->_depth+1);$dt->dump();}}$dir=newDirectoryIterator(dirname(__DIR__));$dt=newDirectoryTraverser($dir);$dt->dump();
输出结果会按照一定的缩进格式进行显示:
1234567891011121314151617181920212223
> php dir.php
string(59)"This is a directory: /home/caiknife/source/phptest/dog-pile"string(64)" This is a file: /home/caiknife/source/phptest/dog-pile/1.php"string(64)" This is a file: /home/caiknife/source/phptest/dog-pile/2.php"string(51)"This is a file: /home/caiknife/source/phptest/1.php"string(57)"This is a directory: /home/caiknife/source/phptest/jquery"string(63)" This is a file: /home/caiknife/source/phptest/jquery/1.html"string(76)" This is a file: /home/caiknife/source/phptest/jquery/jquery.1.8.3.min.js"string(57)"This is a directory: /home/caiknife/source/phptest/remote"string(78)" This is a file: /home/caiknife/source/phptest/remote/file_get_contents.php"string(65)" This is a file: /home/caiknife/source/phptest/remote/curl.php"string(54)"This is a directory: /home/caiknife/source/phptest/spl"string(61)" This is a file: /home/caiknife/source/phptest/spl/dir.php"string(62)" This is a file: /home/caiknife/source/phptest/spl/heap.php"string(51)"This is a file: /home/caiknife/source/phptest/2.php"string(53)"This is a file: /home/caiknife/source/phptest/apc.php"string(62)"This is a directory: /home/caiknife/source/phptest/magicmethod"string(67)" This is a file: /home/caiknife/source/phptest/magicmethod/1.php"string(67)" This is a file: /home/caiknife/source/phptest/magicmethod/2.php"string(67)" This is a file: /home/caiknife/source/phptest/magicmethod/3.php"string(67)" This is a file: /home/caiknife/source/phptest/magicmethod/5.php"string(67)" This is a file: /home/caiknife/source/phptest/magicmethod/6.php"string(67)" This is a file: /home/caiknife/source/phptest/magicmethod/4.php"