您的位置:首页 > 编程语言 > PHP开发

单例模式&&遍历指定目录下的所有文件以及文件夹

2016-10-30 16:54 746 查看
class readDirList

{
    //单例模式 类对象存放空间
    private static $_instance;
  
    //将文件夹和目录存放在字符串中
    private static $_list;
   
    //将文件夹和目录存放在数组中

    private static $_array = array();

    //私有化构造函数

    private function __construct(){}

    //私有化Clone函数

    private function __clone(){}

    //获取类对象接口

    public static function getInstance()
    {
        if(!(self::$_instance instanceof self))
        {
            self::$_instance = new self;
        }
        return self::$_instance;

    }

    //获取目录列表

    public static function getDirList($dir)

    {
       $dir = rtrim($dir,"/");

        if(is_dir($dir))
        {
            if($dh = opendir($dir))
            {
                while($file = readdir($dh))

                {
                    if(is_dir($dir."/".$file) && $file!="." && $file!="..")

                    {

                        array_push(self::$_array,"目录: ".$dir."/".$file);
                        self::$_list .= "目录: ".$dir."/".$file."\r\n";
                        self::getDirList($dir."/".$file);

                    }
                    else if($file!="." && $file!="..")
                    {

                        array_push(self::$_array,"文件: ".$dir."/".$file);
                        self::$_list .= "文件: ".$dir."/".$file."\r\n";

                    }
                }
                closedir($dh);
            }

        }

        return self::$_array;

    }

    //打印对象

    public function show($data)
    {
        echo "<pre>";var_dump($data);echo "</pre>";

    }
}

$filename  = $_SERVER["DOCUMENT_ROOT"].'/‘;  //需要遍历的目录

$readDirList = readDirList::getInstance();
$list = $readDirList->getDirList($filename);

$readDirList->show($list);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐