Home >Backend Development >PHP Tutorial >PHP small code----Read subfiles or subdirectories in a directory_PHP tutorial

PHP small code----Read subfiles or subdirectories in a directory_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 16:53:55950browse

php small code----read sub-files or sub-directories in the directory

class 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:
                                                                                                                                                                     
$dirInfo[] = $fullPath ? $parentPath . DIRECTORY_SEPARATOR . $childDirOrFileName : $childDirOrFileName;
          } else {
&&
                                                                                                                                                                                                                                        $dirInfo[] = $fullPath ? $parentPath . DIRECTORY_SEPARATOR . $childDirOrFileName : $childDirOrFileName;
                }
          }
break;
case self::RECDIR_DIR:
if (IS_DIR ($ Parentpath. Directory_separators. $ ChilddirorFilename)) {
                                                                                                                                                                         
                                                                                                                                                                                                                                        $dirInfo[] = $fullPath ? $parentPath . DIRECTORY_SEPARATOR . $childDirOrFileName : $childDirOrFileName;
                } else {
&&
$ Dirinfo [] = $ Fullpath? $ Parentpath. Directory_Seplator.
                                                                                                                                                                                  
                }
          }
break;
case self::RECDIR_FILE:
if (IS_FILE ($ Parentpath. Directory_separators. $ ChilddirorFilename)) {
                        $dirInfo[] = $fullPath ? $parentPath . DIRECTORY_SEPARATOR . $childDirOrFileName : $childDirOrFileName;
                    }
                    break;
            }
        }
        return $dirInfo;
    }
     /**
* (PHP 5 >= 5.4.0)
* Get the direct subdirectory or direct subfile information under the directory
* @param string $modeInfo[optional]

* Return the mode of information in the directory
* mixed Returns all file names and directory names
* dir Returns all directory names
* file Returns all file names
*

* @param bool $defaultDir[optional]

* Whether to include the default link directory.. and.
* false does not include
* true includes
*

* @param bool $fullPath[optional]

* Whether to return the path information of sub-files or directories
* true is
* false No
*

* @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 '在文件【' . $ex->getFile() . '】中的第' . $ex->getLine() . '行报错:' . $ex->getMessage() . '(' . $ex->getCode() . ')';
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1053347.htmlTechArticlephp小代码----目录下读取子文件或子目录 ?php class RecDir { protected $rootPath; protected $opDirectory; const RECDIR_MIXED = mixed; const RECDIR_DIR = dir; const RECD...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn