Dir.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. namespace app\common\util;
  3. class Dir {
  4. private $_values = array();
  5. public $error = "";
  6. /**
  7. * 架构函数
  8. * @param string $path 目录路径
  9. */
  10. public function __construct($path = '', $pattern = '*') {
  11. if (!$path) return false;
  12. if (substr($path, -1) != "/") $path .= "/";
  13. $this->listFile($path, $pattern);
  14. }
  15. /**
  16. * 生成目录
  17. * @param string $path 目录
  18. * @param integer $mode 权限
  19. * @return boolean
  20. */
  21. public static function create($path, $mode = 0755) {
  22. if(is_dir($path)) return TRUE;
  23. $path = str_replace("\\", "/", $path);
  24. if(substr($path, -1) != '/') $path = $path.'/';
  25. $temp = explode('/', $path);
  26. $cur_dir = '';
  27. $max = count($temp) - 1;
  28. for($i=0; $i<$max; $i++) {
  29. $cur_dir .= $temp[$i].'/';
  30. if (@is_dir($cur_dir)) continue;
  31. @mkdir($cur_dir, $mode, true);
  32. @chmod($cur_dir, $mode);
  33. }
  34. return is_dir($path);
  35. }
  36. /**
  37. * 取得目录下面的文件信息
  38. * @param mixed $pathname 路径
  39. */
  40. public static function listFile($pathname, $pattern = '*') {
  41. static $_listDirs = array();
  42. $guid = md5($pathname . $pattern);
  43. if (!isset($_listDirs[$guid])) {
  44. $dir = array();
  45. $list = glob($pathname . $pattern);
  46. foreach ($list as $i => $file) {
  47. //$dir[$i]['filename'] = basename($file);
  48. //basename取中文名出问题.改用此方法
  49. //编码转换.把中文的调整一下.
  50. $dir[$i]['filename'] = preg_replace('/^.+[\\\\\\/]/', '', $file);
  51. $dir[$i]['pathname'] = realpath($file);
  52. $dir[$i]['owner'] = fileowner($file);
  53. $dir[$i]['perms'] = fileperms($file);
  54. $dir[$i]['inode'] = fileinode($file);
  55. $dir[$i]['group'] = filegroup($file);
  56. $dir[$i]['path'] = dirname($file);
  57. $dir[$i]['atime'] = fileatime($file);
  58. $dir[$i]['ctime'] = filectime($file);
  59. $dir[$i]['size'] = filesize($file);
  60. $dir[$i]['type'] = filetype($file);
  61. $dir[$i]['ext'] = is_file($file) ? strtolower(substr(strrchr(basename($file), '.'), 1)) : '';
  62. $dir[$i]['mtime'] = filemtime($file);
  63. $dir[$i]['isDir'] = is_dir($file);
  64. $dir[$i]['isFile'] = is_file($file);
  65. $dir[$i]['isLink'] = is_link($file);
  66. //$dir[$i]['isExecutable']= function_exists('is_executable')?is_executable($file):'';
  67. $dir[$i]['isReadable'] = is_readable($file);
  68. $dir[$i]['isWritable'] = is_writable($file);
  69. }
  70. $cmp_func = create_function('$a,$b', '
  71. $k = "isDir";
  72. if($a[$k] == $b[$k]) return 0;
  73. return $a[$k]>$b[$k]?-1:1;
  74. ');
  75. // 对结果排序 保证目录在前面
  76. usort($dir, $cmp_func);
  77. $this->_values = $dir;
  78. $_listDirs[$guid] = $dir;
  79. } else {
  80. $this->_values = $_listDirs[$guid];
  81. }
  82. }
  83. /**
  84. * 返回数组中的当前元素(单元)
  85. * @return array
  86. */
  87. public static function current($arr) {
  88. if (!is_array($arr)) {
  89. return false;
  90. }
  91. return current($arr);
  92. }
  93. /**
  94. * 文件上次访问时间
  95. * @return integer
  96. */
  97. public static function getATime() {
  98. $current = $this->current($this->_values);
  99. return $current['atime'];
  100. }
  101. /**
  102. * 取得文件的 inode 修改时间
  103. * @return integer
  104. */
  105. public static function getCTime() {
  106. $current = $this->current($this->_values);
  107. return $current['ctime'];
  108. }
  109. /**
  110. * 遍历子目录文件信息
  111. * @return DirectoryIterator
  112. */
  113. public static function getChildren() {
  114. $current = $this->current($this->_values);
  115. if ($current['isDir']) {
  116. return new Dir($current['pathname']);
  117. }
  118. return false;
  119. }
  120. /**
  121. * 取得文件名
  122. * @return string
  123. */
  124. public static function getFilename() {
  125. $current = $this->current($this->_values);
  126. return $current['filename'];
  127. }
  128. /**
  129. * 取得文件的组
  130. * @return integer
  131. */
  132. public static function getGroup() {
  133. $current = $this->current($this->_values);
  134. return $current['group'];
  135. }
  136. /**
  137. * 取得文件的 inode
  138. * @return integer
  139. */
  140. public static function getInode() {
  141. $current = $this->current($this->_values);
  142. return $current['inode'];
  143. }
  144. /**
  145. * 取得文件的上次修改时间
  146. * @return integer
  147. */
  148. public static function getMTime() {
  149. $current = $this->current($this->_values);
  150. return $current['mtime'];
  151. }
  152. /**
  153. * 取得文件的所有者
  154. * @return string
  155. */
  156. function getOwner() {
  157. $current = $this->current($this->_values);
  158. return $current['owner'];
  159. }
  160. /**
  161. * 取得文件路径,不包括文件名
  162. * @return string
  163. */
  164. public static function getPath() {
  165. $current = $this->current($this->_values);
  166. return $current['path'];
  167. }
  168. /**
  169. * 取得文件的完整路径,包括文件名
  170. * @return string
  171. */
  172. public static function getPathname() {
  173. $current = $this->current($this->_values);
  174. return $current['pathname'];
  175. }
  176. /**
  177. * 取得文件的权限
  178. * @return integer
  179. */
  180. public static function getPerms() {
  181. $current = $this->current($this->_values);
  182. return $current['perms'];
  183. }
  184. /**
  185. * 取得文件的大小
  186. * @return integer
  187. */
  188. public static function getSize() {
  189. $current = $this->current($this->_values);
  190. return $current['size'];
  191. }
  192. /**
  193. * 取得文件类型
  194. * @return string
  195. */
  196. public static function getType() {
  197. $current = $this->current($this->_values);
  198. return $current['type'];
  199. }
  200. /**
  201. * 是否为目录
  202. * @return boolen
  203. */
  204. public static function isDir() {
  205. $current = $this->current($this->_values);
  206. return $current['isDir'];
  207. }
  208. /**
  209. * 是否为文件
  210. * @return boolen
  211. */
  212. public static function isFile() {
  213. $current = $this->current($this->_values);
  214. return $current['isFile'];
  215. }
  216. /**
  217. * 文件是否为一个符号连接
  218. * @return boolen
  219. */
  220. public static function isLink() {
  221. $current = $this->current($this->_values);
  222. return $current['isLink'];
  223. }
  224. /**
  225. * 文件是否可以执行
  226. * @return boolen
  227. */
  228. public static function isExecutable() {
  229. $current = $this->current($this->_values);
  230. return $current['isExecutable'];
  231. }
  232. /**
  233. * 文件是否可读
  234. * @return boolen
  235. */
  236. public static function isReadable() {
  237. $current = $this->current($this->_values);
  238. return $current['isReadable'];
  239. }
  240. /**
  241. * 获取foreach的遍历方式
  242. * @return string
  243. */
  244. public static function getIterator() {
  245. return new ArrayObject($this->_values);
  246. }
  247. // 返回目录的数组信息
  248. public static function toArray() {
  249. return $this->_values;
  250. }
  251. // 静态方法
  252. /**
  253. * 判断目录是否为空
  254. * @return void
  255. */
  256. public static function isEmpty($directory) {
  257. $handle = opendir($directory);
  258. while (($file = readdir($handle)) !== false) {
  259. if ($file != "." && $file != "..") {
  260. closedir($handle);
  261. return false;
  262. }
  263. }
  264. closedir($handle);
  265. return true;
  266. }
  267. /**
  268. * 取得目录中的结构信息
  269. * @return void
  270. */
  271. public static function getList($directory) {
  272. $scandir = scandir($directory);
  273. $dir = [];
  274. foreach ($scandir as $k => $v) {
  275. if ($v == '.' || $v == '..') {
  276. continue;
  277. }
  278. $dir[] = $v;
  279. }
  280. return $dir;
  281. }
  282. /**
  283. * 删除目录(包括下面的文件)
  284. * @return void
  285. */
  286. public static function delDir($directory, $subdir = true) {
  287. if (is_dir($directory) == false) {
  288. return false;
  289. }
  290. $handle = opendir($directory);
  291. while (($file = readdir($handle)) !== false) {
  292. if ($file != "." && $file != "..") {
  293. is_dir("$directory/$file") ?
  294. Dir::delDir("$directory/$file") :
  295. @unlink("$directory/$file");
  296. }
  297. }
  298. if (readdir($handle) == false) {
  299. closedir($handle);
  300. rmdir($directory);
  301. }
  302. }
  303. /**
  304. * 删除目录下面的所有文件,但不删除目录
  305. * @return void
  306. */
  307. public static function del($directory) {
  308. if (is_dir($directory) == false) {
  309. return false;
  310. }
  311. $handle = opendir($directory);
  312. while (($file = readdir($handle)) !== false) {
  313. if ($file != "." && $file != ".." && is_file("$directory/$file")) {
  314. unlink("$directory/$file");
  315. }
  316. }
  317. closedir($handle);
  318. }
  319. /**
  320. * 复制目录
  321. * @return void
  322. */
  323. public static function copyDir($source, $destination) {
  324. if (is_dir($source) == false) {
  325. return false;
  326. }
  327. if (is_dir($destination) == false) {
  328. mkdir($destination, 0755);
  329. }
  330. $handle = opendir($source);
  331. while (false !== ($file = readdir($handle))) {
  332. if ($file != "." && $file != "..") {
  333. if (is_dir("$source/$file")) {
  334. Dir::copyDir("$source/$file", "$destination/$file");
  335. } else {
  336. copy("$source/$file", "$destination/$file");
  337. }
  338. }
  339. }
  340. closedir($handle);
  341. }
  342. }
  343. ?>