php小代码----目录下读取子文件或子目录

<?php
 
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 = '您提供的目录不存在!';
            $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/>
      * 得到目录下的直接子目录或直接子文件信息
      * @param string $modeInfo[可选]<p>
      * 返回目录下信息的模式
      * mixed  返回所有的文件名及目录名
      * dir    返回所有的目录名
      * file   返回所有的文件名
      * </p>
      * @param bool $defaultDir[可选]<p>
      * 是否包括默认的链接目录..和.
      * false  不包括
      * true   包括
      * </p>
      * @param bool $fullPath[可选]<p>
      * 是否返回子文件或目录的路径信息
      * true  是
      * false 否
      * <p>
      * @return array 返回一个数组,记录了该目录下的信息
      */
    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() . ')';
}

时间: 2024-08-03 22:43:11

php小代码----目录下读取子文件或子目录的相关文章

hadoop修改哪个参数可以使fileinputformat可以读取子文件夹下的内容

问题描述 hadoop修改哪个参数可以使fileinputformat可以读取子文件夹下的内容 hadoop修改哪个参数可以使fileinputformat可以读取子文件夹下的内容

文件读写-如何在工程目录下读取.c的源文件

问题描述 如何在工程目录下读取.c的源文件 如何在工程目录下读取.c的源文件 然后分析文件中是否有"/* ""//" 解决方案 #include int main() { FILE *fp,*fp1; char str[99]=""; int i=0; fp=fopen("test.c","r"); //要打开的源文件 fp1=fopen("new.c","w");

index-JSP跳转同一文件夹下的子文件夹地址怎么写;

问题描述 JSP跳转同一文件夹下的子文件夹地址怎么写: 例如从WebRoot下的index页面跳转到WebRoot/admin/mainpage.jsp.怎么跳 request.getRequestDispatcher("/admin/main.jsp").forward(request, response); 我这样写.出现了错误. 解决方案 如果回答对你有帮助,请采纳 解决方案二: 解决方案三: 跳到main还是mainpage 把admin前面的/去掉试一下

LINUX find 命令怎么才能只找当先目录下的文件而不找子目录下的相同文件

问题描述 小弟目录如下 /home/mytest下面有4个包文件.zip,该目录下还有2011/02-2011目录这个目录下you同样的4个zip文件.问题:我用find . -name "*.zip" -print 查找,结果会打印出mytest 目录及子目录下的文件,我只想要4个可出来的是8个求解 怎么才能让只出来当前目录下的4个 问题补充:deepfuture 写道 解决方案 加个-maxdepth n参数指定查找最大深度就可以了find . -maxdepth 1 -name

python获取某目录下的所有文件和子目录-os.listdir

有时候需要获取某目录下的所有文件和子目录,从stackoverflow搜到了一段代码,需要时可以使用. 在Python中可以使用os.listdir()函数获得指定目录中的内容.其原型如下所示.   os.listdir(path)   其参数含义如下.path 要获得内容目录的路径.以下实例获得当前目录的内容.   >>> import os    >>> os.listdir(os.getcwd())    获得当前目录中的内容   ['dde.pyd', 'lic

获取某目录下的所有文件(包括子目录下文件)的数量(C#)

以前写过一个通过计算目录遍历所有文件和子目录的方法来获得某目录下的文件个数,结果发现速度极慢,远远不及系统本身目录属性里边显示的速度. int fileNum = 0;/**//// <summary>/// 获取某目录下的所有文件(包括子目录下文件)的数量/// </summary>/// <param name="srcPath"></param>/// <returns></returns>public in

读取目录下的所有文件(包括子目录下的所有文件)

****************************** Many times we might need some part of code which will access all sub-folders of the server and also all files within the sub-folder. The following line of asp code will map to a specified folder and searches all the sub

C#实现的列出目录下所有子目录和文件的程序(附思路)

程序 把自己的资料刻录成很多光盘,发现连自己都很难找到需要的文件在哪张光盘上,因此我就根据需求,在Visual Studio.NET中写了一个列出目录下所有子目录和文件的程序,以方便我列出刻录的资料光盘上的所有文件信息. 本程序的主要算法是递归,主函数如下: //递归列出目录下的所有文件和子目录 public void ListFiles( FileSystemInfo fileinfo ) { if( ! fileinfo.Exists ) return; DirectoryInfo diri

Python实现扫描指定目录下的子目录及文件的方法_python

本文介绍了使用Python来扫描指定目录下的文件,或者匹配指定后缀和前缀的函数.步骤如下: 如果要扫描指定目录下的文件,包括子目录,需要调用scan_files("/export/home/test/") 如果要扫描指定目录下的特定后缀的文件(比如jar包),包括子目录,调用scan_files("/export/home/test/", postfix=".jar") 如果要扫描指定目录下的特定前缀的文件(比如test_xxx.py),包括子目