123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- <?php
- /**
- * 小梦科技资源nanodreamtech.com
- *
- * ==========================================================================
- * @link https://www.nanodreamtech.com/
- * @copyright Copyright (c) 2015 liofis.com.
- * @license https://www.nanodreamtech.com/license.html License
- * ==========================================================================
- *
- * @author fish
- *
- */
- namespace Lib;
- class Dir {
- private $_values = array();
- public $error = "";
- /**
- +----------------------------------------------------------
- * 架构函数
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $path 目录路径
- +----------------------------------------------------------
- */
- function __construct($path='./', $pattern = '*') {
- if (substr($path, -1) != "/") {
- $path .= "/";
- }
- $this->listFile($path, $pattern);
- }
- /**
- +----------------------------------------------------------
- * 取得目录下面的文件信息
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param mixed $pathname 路径
- +----------------------------------------------------------
- */
- function listFile($pathname, $pattern = '*') {
- static $_listDirs = array();
- $guid = md5($pathname . $pattern);
- if (!isset($_listDirs[$guid])) {
- $dir = array();
- $list = glob($pathname . $pattern);
- foreach ($list as $i => $file) {
- //$dir[$i]['filename'] = basename($file);
- //basename取中文名出问题.改用此方法
- //编码转换.把中文的调整一下.
- $dir[$i]['filename'] = preg_replace('/^.+[\\\\\\/]/', '', $file);
- $dir[$i]['pathname'] = realpath($file);
- $dir[$i]['owner'] = fileowner($file);
- $dir[$i]['perms'] = fileperms($file);
- $dir[$i]['inode'] = fileinode($file);
- $dir[$i]['group'] = filegroup($file);
- $dir[$i]['path'] = dirname($file);
- $dir[$i]['atime'] = fileatime($file);
- $dir[$i]['ctime'] = filectime($file);
- $dir[$i]['size'] = filesize($file);
- $dir[$i]['type'] = filetype($file);
- $dir[$i]['ext'] = is_file($file) ? strtolower(substr(strrchr(basename($file), '.'), 1)) : '';
- $dir[$i]['mtime'] = filemtime($file);
- $dir[$i]['isDir'] = is_dir($file);
- $dir[$i]['isFile'] = is_file($file);
- $dir[$i]['isLink'] = is_link($file);
- //$dir[$i]['isExecutable']= function_exists('is_executable')?is_executable($file):'';
- $dir[$i]['isReadable'] = is_readable($file);
- $dir[$i]['isWritable'] = is_writable($file);
- }
- $cmp_func = create_function('$a,$b', '
- $k = "isDir";
- if($a[$k] == $b[$k]) return 0;
- return $a[$k]>$b[$k]?-1:1;
- ');
- // 对结果排序 保证目录在前面
- usort($dir, $cmp_func);
- $this->_values = $dir;
- $_listDirs[$guid] = $dir;
- } else {
- $this->_values = $_listDirs[$guid];
- }
- }
- /**
- +----------------------------------------------------------
- * 返回数组中的当前元素(单元)
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return array
- +----------------------------------------------------------
- */
- function current($arr) {
- if (!is_array($arr)) {
- return false;
- }
- return current($arr);
- }
- /**
- +----------------------------------------------------------
- * 文件上次访问时间
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return integer
- +----------------------------------------------------------
- */
- function getATime() {
- $current = $this->current($this->_values);
- return $current['atime'];
- }
- /**
- +----------------------------------------------------------
- * 取得文件的 inode 修改时间
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return integer
- +----------------------------------------------------------
- */
- function getCTime() {
- $current = $this->current($this->_values);
- return $current['ctime'];
- }
- /**
- +----------------------------------------------------------
- * 遍历子目录文件信息
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return DirectoryIterator
- +----------------------------------------------------------
- */
- function getChildren() {
- $current = $this->current($this->_values);
- if ($current['isDir']) {
- return new Dir($current['pathname']);
- }
- return false;
- }
- /**
- +----------------------------------------------------------
- * 取得文件名
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- function getFilename() {
- $current = $this->current($this->_values);
- return $current['filename'];
- }
- /**
- +----------------------------------------------------------
- * 取得文件的组
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return integer
- +----------------------------------------------------------
- */
- function getGroup() {
- $current = $this->current($this->_values);
- return $current['group'];
- }
- /**
- +----------------------------------------------------------
- * 取得文件的 inode
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return integer
- +----------------------------------------------------------
- */
- function getInode() {
- $current = $this->current($this->_values);
- return $current['inode'];
- }
- /**
- +----------------------------------------------------------
- * 取得文件的上次修改时间
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return integer
- +----------------------------------------------------------
- */
- function getMTime() {
- $current = $this->current($this->_values);
- return $current['mtime'];
- }
- /**
- +----------------------------------------------------------
- * 取得文件的所有者
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- function getOwner() {
- $current = $this->current($this->_values);
- return $current['owner'];
- }
- /**
- +----------------------------------------------------------
- * 取得文件路径,不包括文件名
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- function getPath() {
- $current = $this->current($this->_values);
- return $current['path'];
- }
- /**
- +----------------------------------------------------------
- * 取得文件的完整路径,包括文件名
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- function getPathname() {
- $current = $this->current($this->_values);
- return $current['pathname'];
- }
- /**
- +----------------------------------------------------------
- * 取得文件的权限
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return integer
- +----------------------------------------------------------
- */
- function getPerms() {
- $current = $this->current($this->_values);
- return $current['perms'];
- }
- /**
- +----------------------------------------------------------
- * 取得文件的大小
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return integer
- +----------------------------------------------------------
- */
- function getSize() {
- $current = $this->current($this->_values);
- return $current['size'];
- }
- /**
- +----------------------------------------------------------
- * 取得文件类型
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- function getType() {
- $current = $this->current($this->_values);
- return $current['type'];
- }
- /**
- +----------------------------------------------------------
- * 是否为目录
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- */
- function isDir() {
- $current = $this->current($this->_values);
- return $current['isDir'];
- }
- /**
- +----------------------------------------------------------
- * 是否为文件
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- */
- function isFile() {
- $current = $this->current($this->_values);
- return $current['isFile'];
- }
- /**
- +----------------------------------------------------------
- * 文件是否为一个符号连接
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- */
- function isLink() {
- $current = $this->current($this->_values);
- return $current['isLink'];
- }
- /**
- +----------------------------------------------------------
- * 文件是否可以执行
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- */
- function isExecutable() {
- $current = $this->current($this->_values);
- return $current['isExecutable'];
- }
- /**
- +----------------------------------------------------------
- * 文件是否可读
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- */
- function isReadable() {
- $current = $this->current($this->_values);
- return $current['isReadable'];
- }
- /**
- +----------------------------------------------------------
- * 获取foreach的遍历方式
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- function getIterator() {
- return new ArrayObject($this->_values);
- }
- // 返回目录的数组信息
- function toArray() {
- return $this->_values;
- }
- // 静态方法
- /**
- +----------------------------------------------------------
- * 判断目录是否为空
- +----------------------------------------------------------
- * @access static
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- function isEmpty($directory) {
- $handle = opendir($directory);
- while (($file = readdir($handle)) !== false) {
- if ($file != "." && $file != "..") {
- closedir($handle);
- return false;
- }
- }
- closedir($handle);
- return true;
- }
- /**
- +----------------------------------------------------------
- * 取得目录中的结构信息
- +----------------------------------------------------------
- * @access static
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- function getList($directory) {
- return scandir($directory);
- }
- /**
- +----------------------------------------------------------
- * 删除目录(包括下面的文件)
- +----------------------------------------------------------
- * @access static
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- function delDir($directory, $subdir = true) {
- if (is_dir($directory) == false) {
- $this->error = "该目录是不存在!";
- return false;
- }
- $handle = opendir($directory);
- while (($file = readdir($handle)) !== false) {
- if ($file != "." && $file != "..") {
- is_dir("$directory/$file") ?
- Dir::delDir("$directory/$file") :
- unlink("$directory/$file");
- }
- }
- if (readdir($handle) == false) {
- closedir($handle);
- rmdir($directory);
- }
- }
- /**
- +----------------------------------------------------------
- * 删除目录下面的所有文件,但不删除目录
- +----------------------------------------------------------
- * @access static
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- function del($directory) {
- if (is_dir($directory) == false) {
- $this->error = "该目录是不存在!";
- return false;
- }
- $handle = opendir($directory);
- while (($file = readdir($handle)) !== false) {
- if ($file != "." && $file != ".." && is_file("$directory/$file")) {
- unlink("$directory/$file");
- }
- }
- closedir($handle);
- }
- /**
- +----------------------------------------------------------
- * 复制目录
- +----------------------------------------------------------
- * @access static
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- function copyDir($source, $destination) {
- if (is_dir($source) == false) {
- $this->error = "源目录不存在!";
- return false;
- }
- if (is_dir($destination) == false) {
- mkdir($destination, 0700);
- }
- $handle = opendir($source);
- while (false !== ($file = readdir($handle))) {
- if ($file != "." && $file != "..") {
- is_dir("$source/$file") ?
- Dir::copyDir("$source/$file", "$destination/$file") :
- copy("$source/$file", "$destination/$file");
- }
- }
- closedir($handle);
- }
- }
|