PHP Small Code----read sub files or subdirectories under directory

Source: Internet
Author: User
Tags array bool mixed return
<?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;           &nbsp 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 (). ')'; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.