zoukankan      html  css  js  c++  java
  • php读取目录及子目录下所有文件名的方法

    为了便于操作,先将PHP读取目录及子目录下所有文件名的方法封装成一个类。

    // +----------------------------------------------------------------------
    // | lidequan [ I CAN DO IT JUST WORK HARD ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2016 http://www.findme.wang All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: lidequan <dequanLi_edu@126.com> 
    // +----------------------------------------------------------------------
    class File{
        /**
        *获取某个目录下所有文件
        *@param $path文件路径
        *@param $child 是否包含对应的目录
        */
        public  function getFiles($path,$child=false){
            $files=array();        
            if(!$child){
                if(is_dir($path)){
                    $dp = dir($path); 
                }else{
                    return null;
                }
                while ($file = $dp ->read()){  
                    if($file !="." && $file !=".." && is_file($path.$file)){  
                       $files[] = $file;
                    }  
                }           
                $dp->close();
            }else{
                $this->scanfiles($files,$path);
            }              
            return $files;
        }
        /**
        *@param $files 结果
        *@param $path 路径
        *@param $childDir 子目录名称
        */
        public function scanfiles(&$files,$path,$childDir=false){
            $dp = dir($path); 
            while ($file = $dp ->read()){  
                if($file !="." && $file !=".."){ 
                    if(is_file($path.$file)){//当前为文件
                         $files[]= $file;
                    }else{//当前为目录  
                         $this->scanfiles($files[$file],$path.$file.DIRECTORY_SEPARATOR,$file);
                    }               
                } 
            }
            $dp->close();
        }
    }

    读取Manual/html目录及子目录下所有文件名

    $File=new File();
    $info=$File->getFiles('Manual/html/',true);

    这里写图片描述 
    读取Manual/html目录下所有文件名

    $File=new File();
    $info=$File->getFiles('Manual/html/');

    这里写图片描述

    也可以用php中glob与scandir,当时其有一定的限制。

     
     
  • 相关阅读:
    bzoj 3438: 小M的作物
    bzoj 4445 [SCOI2015] 小凸想跑步
    hdu 4899 Hero meet devil
    hdu 4898 The Revenge of the Princess’ Knight
    【NOIP1999】拦截导弹
    【OpenJudge】2991:2011 题解
    【cqbzoj】1785:残缺棋盘上放车的方案数 --状压dp --输入毁一生
    【cqbzoj】:1330 Prime DP(Ahio2001 质数和分解)
    【Openjudge:Noi】7891:一元三次方程求解 c++
    【USACO FEB 2010 SILVER】吃巧克力(Chocolate Eating)
  • 原文地址:https://www.cnblogs.com/as3lib/p/6859307.html
Copyright © 2011-2022 走看看