PHP Small Code----read sub files or subdirectories under directory
Last Update:2017-02-28
Source: Internet
Author: User
<?php class RecDir { protected $rootPath; protected $opDirectory; &NB Sp 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 provide does not exist! '; $this->errorno = 1001;   throw new Exception ($this->errormsg, $this->errorno); } , Private Function Read ($directory, $parentPath, $MODEINF o = ' mixed ', $defaultDir = False, $fullPath = False) { $dirInfo = array (); &NBSP ; while (FALSE!== ($childDirOrFileName = $directory->read ()) { switch ($modeInfo) { case self::recdir_mixed: &NBS P if ($defaultDir) { &NBSP ; $dirInfo [] = $fullPath? $parentPath. Directory_separator. $childDirOrFileName: $childDirOrFileName; } else { & nbsp if ($childDirOrFileName!= '. ')&& $childDirOrFileName!= ' ... ') { $dirInfo [] = $fullPath ? $parentPath. Directory_separator. $childDirOrFileName: $childDirOrFileName; & nbsp } break; case Self::recdir_dir: & nbsp if (Is_dir $parentPath. Directory_separator. $childDirOrFileName)) { if ($defaultDi R) { $dirInfo [] = $fullP Ath? $parentPath. Directory_separator. $childDirOrFileName: $childDirOrFileNaMe } else { & nbsp $childDirOrFileName!= '. ' && $childDirOrFileNa Me!= ' ... ') { $dirInfo [] = $fullPath? $parentPath. Directory_separator. $childDirOrFileName: $childDirOrFileName; } & nbsp & nbsp } break; case Self::recdir_file: if (Is_file $parentPath. DirecTory_separator. $childDirOrFileName)) { $dirInfo [] = $ FullPath? $parentPath. Directory_separator. $childDirOrFileName: $childDirOrFileName; & nbsp break; { return $dirInfo; } /** * (PHP 5 >= 5.4.0) <br/> * Get directory Direct subdirectory or direct file information * @param string $modeInfo [optional]<p> * Return to directory information in the mode of * Mixed return all filename and directory name * dir Return all directory name * file return Some filename * </p> * @param bool $defaultDir [optional]<p> * Include the default link directory ... And. * false not including * True including * </p> &NBS P * @param bool $fullPath [optional]<p> * Whether to return the path information of the child file or directory * true is &NB Sp * False no * <p> * @return Array returns an array that records the information in this directory &NB Sp * 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 ', tr UE, TRUE); Var_dump ($dirs); catch (Exception $ex) { echo ' in file '. $ex->getfile (). ' The first ' in '. $ex->getline (). ' Line error: '. $ex->getmessage (). ' ('. $ex->getcode (). ')'; }