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,当时其有一定的限制。

     
     
  • 相关阅读:
    Leetcode.11 Container with Most Water
    Leetcode.19 Remove Nth Node From End of List
    Leetcode23. Merge K sorted List
    leetcode287. Find the duplicate Number
    LeetCode234. Palindrome Linked List
    leetcode.142 LinkedList Cycle II
    UINavigationController
    UITableView的性能优化1
    iOS触摸事件
    UITableView的性能优化
  • 原文地址:https://www.cnblogs.com/as3lib/p/6859307.html
Copyright © 2011-2022 走看看