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

Source: Internet
Author: User

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

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.