Home > Backend Development > PHP Tutorial > PHP获取文件夹内所有文件包括子目录文件的名称或路径

PHP获取文件夹内所有文件包括子目录文件的名称或路径

WBOY
Release: 2016-06-23 13:26:11
Original
924 people have browsed it

/**  new getFile($_dir[,$_emptyDir,$_fileType]);* @parma $_dir 是目录名称* @parma $_emptyDir 是否获取空文件夹,选填,默认不获取,true则获取空文件夹* @parma $_fileType 获取文件名称的类型,选填,默认获取只文件名称,true则获取带有路径的文件名称**/class getFiles {    private $_dir;    private $_emptyDir;    private $_fileType;    public $_files;         public function __construct($_dir,$_emptyDir = false,$_fileType = false) {        $this->_dir = $_dir;        $this->_emptyDir = $_emptyDir;        $this->_fileType = $_fileType;        if ($this->_emptyDir) {            $this->getFileEmpty($this->_dir,$this->_files);        } else {            $this->getFile($this->_dir,$this->_files);        }    }    //读取文件夹所有文件不包括空文件夹    private function getFile($_dir,&$_arr) {        if (!!$_dirs = opendir($_dir)) {            while (($_file=readdir($_dirs)) != false) {                if ($_file == '.' || $_file == '..') continue;                $_files = $_dir .'/'. $_file;                if (is_dir($_files)) {                    $this->getFile($_files,$_arr);                } else {                    if ($this->_fileType) {                        $_arr[] = $_files;                    } else {                        $_arr[] = $_file;                    }                }            }        }        closedir($_dirs);    }    //读取文件夹所有文件包括空文件夹    private function getFileEmpty($_dir,&$_arr) {        if (!!$_dirs = opendir($_dir)) {            while (($_file=readdir($_dirs)) != false) {                if ($_file == '.' || $_file == '..') continue;                $_files = $_dir .'/'. $_file;                if (is_dir($_files) && $this->isEmpty($_files)) {                    $this->getFileEmpty($_files,$_arr);                } else {                    if ($this->_fileType) {                        $_arr[] = $_files;                    } else {                        $_arr[] = $_file;                    }                }            }        }        closedir($_dirs);    }    //判断文件夹是否为空    private function isEmpty($_dir) {        if (!!$_dirs = opendir($_dir)) {            while (($_file=readdir($_dirs)) != false) {                if($_file != '.' && $_file != '..') {                    closedir($_dirs);                    return true;                    break;                }            }            closedir($_dirs);            return false;        }    }}
Copy after login

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template