Tag Read sub-file or subdirectory under:php directory
<?phpclass RecDir { protected $rootPath; protected $opDirectory; const recdir_mixed = ' MIXED '; const RECDIR_DIR = ' DIR '; const recdir_file = ' file '; public $errorMsg = '; public $errorNo = 0; public function __construct ($rootPath) { $this->rootpath = $rootPath; if (Is_dir ($this->rootpath)) { $this->rootpath = pathinfo ($this->rootpath, pathinfo_ DIRNAME) . directory_separator . pathinfo ($this->rootpath, pathinfo_basename); $this->opdirectory = dir ($this->rootpath); } else { $this->errormsg = ' The directory you provided does not exist! '; $this->errorno = 1001; throw new exception ($this- >errorMsg, $this->errorno); } } private function read ($directory, $parentPath, $modeInfo = ' mixed ', $defaultDir = false, $fullPath = false) { $dirInfo = array (); while (false !== ($childDirOrFileName = $directory->read ())) { switch ($ Modeinfo) { case self::RECDIR_MIXED: if ($defaultDir) { $dirInfo [] = $fullPath ? $parentPath . directory_separator . $ childdirorfilename : $childDirOrFileName; } else { if ($childDirOrFileName != '. ' && $childDirOrFileName != ' ... ') { $dirInfo [] = $fullPath ? $parentPath . DIRECTORY_SEPARATOR . $childDirOrFileName : $ childdirorfilename; } } break; case self::RECDIR_DIR: if (Is_dir ($parentPath . directory_separator . $childDirOrFileName)) { if ($defaultDir) { $dirInfo [] = $fullPath ? $parentPath . DIRECTORY_SEPARATOR . $childDirOrFileName : $childDirOrFileName; } else { if ($childDirOrFileName != '. ' && $childDirOrFileName != ' ... ') { $dirInfo [] = $fullPath ? $parentPath . DIRECTORY_SEPARATOR . $childDirOrFileName : $childDirOrFileName; } } } break; case self::recdir_file: if (Is_file ($parentPath . DIRECTORY_SEPARATOR . $childDirOrFileName)) { $dirInfo [] = $fullPath ? $parentPath . DIRECTORY_SEPARATOR . $childDirOrFileName : $childDirOrFileName; } break; } } return $dirInfo; } /** * (php 5 >= 5.4.0) <br/> * Get direct sub-directory or direct sub-file information under directory * @param string $modeInfo [Optional]<p> * returns the mode of information in the directory * mixed returns all filenames and directory names * dir return all directory names * file return all filenames * </p> * @param bool $defaultDir [optional]<p> * whether to include the default link directory: And . * false do not include * true including * </p> * @param bool $fullPath [optional]<p> * whether to return path information for a child file or directory * true Yes * false no * <p> * @return array Returns an array that records the information in this directory */ public function getpathdirectdirinfo ($ modeinfo = ' mixed ', $defaultDir = false, $fullPath = false) { return $this->read ($this->opdirectory, $this- >rootPath, $modeInfo, $defaultDir, $fullPath); } }//----------------------------Test-----------------------------------------header ("content-type:text/ Html; charset=utf-8 "); try { $recDir = new recdir ('./calltemp/'); $dirs = $ Recdir->getpathdirectdirinfo (' file ', true, true); var_dump ($dirs);} catch (exception $ex) { echo ' in file ' . $ex GetFile () . ' " . $ex->getline () . ' line error: ' . $ex->getmessage () . ' (' . $ex->getcode () . ') ';}
This article is from my It career blog, so be sure to keep this source http://quietnight.blog.51cto.com/7163892/1693468
PHP Small Code----directory to read sub-files or subdirectories