Dir.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Lib;
  15. class Dir {
  16. private $_values = array();
  17. public $error = "";
  18. /**
  19. +----------------------------------------------------------
  20. * 架构函数
  21. +----------------------------------------------------------
  22. * @access public
  23. +----------------------------------------------------------
  24. * @param string $path 目录路径
  25. +----------------------------------------------------------
  26. */
  27. function __construct($path='./', $pattern = '*') {
  28. if (substr($path, -1) != "/") {
  29. $path .= "/";
  30. }
  31. $this->listFile($path, $pattern);
  32. }
  33. /**
  34. +----------------------------------------------------------
  35. * 取得目录下面的文件信息
  36. +----------------------------------------------------------
  37. * @access public
  38. +----------------------------------------------------------
  39. * @param mixed $pathname 路径
  40. +----------------------------------------------------------
  41. */
  42. function listFile($pathname, $pattern = '*') {
  43. static $_listDirs = array();
  44. $guid = md5($pathname . $pattern);
  45. if (!isset($_listDirs[$guid])) {
  46. $dir = array();
  47. $list = glob($pathname . $pattern);
  48. foreach ($list as $i => $file) {
  49. //$dir[$i]['filename'] = basename($file);
  50. //basename取中文名出问题.改用此方法
  51. //编码转换.把中文的调整一下.
  52. $dir[$i]['filename'] = preg_replace('/^.+[\\\\\\/]/', '', $file);
  53. $dir[$i]['pathname'] = realpath($file);
  54. $dir[$i]['owner'] = fileowner($file);
  55. $dir[$i]['perms'] = fileperms($file);
  56. $dir[$i]['inode'] = fileinode($file);
  57. $dir[$i]['group'] = filegroup($file);
  58. $dir[$i]['path'] = dirname($file);
  59. $dir[$i]['atime'] = fileatime($file);
  60. $dir[$i]['ctime'] = filectime($file);
  61. $dir[$i]['size'] = filesize($file);
  62. $dir[$i]['type'] = filetype($file);
  63. $dir[$i]['ext'] = is_file($file) ? strtolower(substr(strrchr(basename($file), '.'), 1)) : '';
  64. $dir[$i]['mtime'] = filemtime($file);
  65. $dir[$i]['isDir'] = is_dir($file);
  66. $dir[$i]['isFile'] = is_file($file);
  67. $dir[$i]['isLink'] = is_link($file);
  68. //$dir[$i]['isExecutable']= function_exists('is_executable')?is_executable($file):'';
  69. $dir[$i]['isReadable'] = is_readable($file);
  70. $dir[$i]['isWritable'] = is_writable($file);
  71. }
  72. $cmp_func = create_function('$a,$b', '
  73. $k = "isDir";
  74. if($a[$k] == $b[$k]) return 0;
  75. return $a[$k]>$b[$k]?-1:1;
  76. ');
  77. // 对结果排序 保证目录在前面
  78. usort($dir, $cmp_func);
  79. $this->_values = $dir;
  80. $_listDirs[$guid] = $dir;
  81. } else {
  82. $this->_values = $_listDirs[$guid];
  83. }
  84. }
  85. /**
  86. +----------------------------------------------------------
  87. * 返回数组中的当前元素(单元)
  88. +----------------------------------------------------------
  89. * @access public
  90. +----------------------------------------------------------
  91. * @return array
  92. +----------------------------------------------------------
  93. */
  94. function current($arr) {
  95. if (!is_array($arr)) {
  96. return false;
  97. }
  98. return current($arr);
  99. }
  100. /**
  101. +----------------------------------------------------------
  102. * 文件上次访问时间
  103. +----------------------------------------------------------
  104. * @access public
  105. +----------------------------------------------------------
  106. * @return integer
  107. +----------------------------------------------------------
  108. */
  109. function getATime() {
  110. $current = $this->current($this->_values);
  111. return $current['atime'];
  112. }
  113. /**
  114. +----------------------------------------------------------
  115. * 取得文件的 inode 修改时间
  116. +----------------------------------------------------------
  117. * @access public
  118. +----------------------------------------------------------
  119. * @return integer
  120. +----------------------------------------------------------
  121. */
  122. function getCTime() {
  123. $current = $this->current($this->_values);
  124. return $current['ctime'];
  125. }
  126. /**
  127. +----------------------------------------------------------
  128. * 遍历子目录文件信息
  129. +----------------------------------------------------------
  130. * @access public
  131. +----------------------------------------------------------
  132. * @return DirectoryIterator
  133. +----------------------------------------------------------
  134. */
  135. function getChildren() {
  136. $current = $this->current($this->_values);
  137. if ($current['isDir']) {
  138. return new Dir($current['pathname']);
  139. }
  140. return false;
  141. }
  142. /**
  143. +----------------------------------------------------------
  144. * 取得文件名
  145. +----------------------------------------------------------
  146. * @access public
  147. +----------------------------------------------------------
  148. * @return string
  149. +----------------------------------------------------------
  150. */
  151. function getFilename() {
  152. $current = $this->current($this->_values);
  153. return $current['filename'];
  154. }
  155. /**
  156. +----------------------------------------------------------
  157. * 取得文件的组
  158. +----------------------------------------------------------
  159. * @access public
  160. +----------------------------------------------------------
  161. * @return integer
  162. +----------------------------------------------------------
  163. */
  164. function getGroup() {
  165. $current = $this->current($this->_values);
  166. return $current['group'];
  167. }
  168. /**
  169. +----------------------------------------------------------
  170. * 取得文件的 inode
  171. +----------------------------------------------------------
  172. * @access public
  173. +----------------------------------------------------------
  174. * @return integer
  175. +----------------------------------------------------------
  176. */
  177. function getInode() {
  178. $current = $this->current($this->_values);
  179. return $current['inode'];
  180. }
  181. /**
  182. +----------------------------------------------------------
  183. * 取得文件的上次修改时间
  184. +----------------------------------------------------------
  185. * @access public
  186. +----------------------------------------------------------
  187. * @return integer
  188. +----------------------------------------------------------
  189. */
  190. function getMTime() {
  191. $current = $this->current($this->_values);
  192. return $current['mtime'];
  193. }
  194. /**
  195. +----------------------------------------------------------
  196. * 取得文件的所有者
  197. +----------------------------------------------------------
  198. * @access public
  199. +----------------------------------------------------------
  200. * @return string
  201. +----------------------------------------------------------
  202. */
  203. function getOwner() {
  204. $current = $this->current($this->_values);
  205. return $current['owner'];
  206. }
  207. /**
  208. +----------------------------------------------------------
  209. * 取得文件路径,不包括文件名
  210. +----------------------------------------------------------
  211. * @access public
  212. +----------------------------------------------------------
  213. * @return string
  214. +----------------------------------------------------------
  215. */
  216. function getPath() {
  217. $current = $this->current($this->_values);
  218. return $current['path'];
  219. }
  220. /**
  221. +----------------------------------------------------------
  222. * 取得文件的完整路径,包括文件名
  223. +----------------------------------------------------------
  224. * @access public
  225. +----------------------------------------------------------
  226. * @return string
  227. +----------------------------------------------------------
  228. */
  229. function getPathname() {
  230. $current = $this->current($this->_values);
  231. return $current['pathname'];
  232. }
  233. /**
  234. +----------------------------------------------------------
  235. * 取得文件的权限
  236. +----------------------------------------------------------
  237. * @access public
  238. +----------------------------------------------------------
  239. * @return integer
  240. +----------------------------------------------------------
  241. */
  242. function getPerms() {
  243. $current = $this->current($this->_values);
  244. return $current['perms'];
  245. }
  246. /**
  247. +----------------------------------------------------------
  248. * 取得文件的大小
  249. +----------------------------------------------------------
  250. * @access public
  251. +----------------------------------------------------------
  252. * @return integer
  253. +----------------------------------------------------------
  254. */
  255. function getSize() {
  256. $current = $this->current($this->_values);
  257. return $current['size'];
  258. }
  259. /**
  260. +----------------------------------------------------------
  261. * 取得文件类型
  262. +----------------------------------------------------------
  263. * @access public
  264. +----------------------------------------------------------
  265. * @return string
  266. +----------------------------------------------------------
  267. */
  268. function getType() {
  269. $current = $this->current($this->_values);
  270. return $current['type'];
  271. }
  272. /**
  273. +----------------------------------------------------------
  274. * 是否为目录
  275. +----------------------------------------------------------
  276. * @access public
  277. +----------------------------------------------------------
  278. * @return boolen
  279. +----------------------------------------------------------
  280. */
  281. function isDir() {
  282. $current = $this->current($this->_values);
  283. return $current['isDir'];
  284. }
  285. /**
  286. +----------------------------------------------------------
  287. * 是否为文件
  288. +----------------------------------------------------------
  289. * @access public
  290. +----------------------------------------------------------
  291. * @return boolen
  292. +----------------------------------------------------------
  293. */
  294. function isFile() {
  295. $current = $this->current($this->_values);
  296. return $current['isFile'];
  297. }
  298. /**
  299. +----------------------------------------------------------
  300. * 文件是否为一个符号连接
  301. +----------------------------------------------------------
  302. * @access public
  303. +----------------------------------------------------------
  304. * @return boolen
  305. +----------------------------------------------------------
  306. */
  307. function isLink() {
  308. $current = $this->current($this->_values);
  309. return $current['isLink'];
  310. }
  311. /**
  312. +----------------------------------------------------------
  313. * 文件是否可以执行
  314. +----------------------------------------------------------
  315. * @access public
  316. +----------------------------------------------------------
  317. * @return boolen
  318. +----------------------------------------------------------
  319. */
  320. function isExecutable() {
  321. $current = $this->current($this->_values);
  322. return $current['isExecutable'];
  323. }
  324. /**
  325. +----------------------------------------------------------
  326. * 文件是否可读
  327. +----------------------------------------------------------
  328. * @access public
  329. +----------------------------------------------------------
  330. * @return boolen
  331. +----------------------------------------------------------
  332. */
  333. function isReadable() {
  334. $current = $this->current($this->_values);
  335. return $current['isReadable'];
  336. }
  337. /**
  338. +----------------------------------------------------------
  339. * 获取foreach的遍历方式
  340. +----------------------------------------------------------
  341. * @access public
  342. +----------------------------------------------------------
  343. * @return string
  344. +----------------------------------------------------------
  345. */
  346. function getIterator() {
  347. return new ArrayObject($this->_values);
  348. }
  349. // 返回目录的数组信息
  350. function toArray() {
  351. return $this->_values;
  352. }
  353. // 静态方法
  354. /**
  355. +----------------------------------------------------------
  356. * 判断目录是否为空
  357. +----------------------------------------------------------
  358. * @access static
  359. +----------------------------------------------------------
  360. * @return void
  361. +----------------------------------------------------------
  362. */
  363. function isEmpty($directory) {
  364. $handle = opendir($directory);
  365. while (($file = readdir($handle)) !== false) {
  366. if ($file != "." && $file != "..") {
  367. closedir($handle);
  368. return false;
  369. }
  370. }
  371. closedir($handle);
  372. return true;
  373. }
  374. /**
  375. +----------------------------------------------------------
  376. * 取得目录中的结构信息
  377. +----------------------------------------------------------
  378. * @access static
  379. +----------------------------------------------------------
  380. * @return void
  381. +----------------------------------------------------------
  382. */
  383. function getList($directory) {
  384. return scandir($directory);
  385. }
  386. /**
  387. +----------------------------------------------------------
  388. * 删除目录(包括下面的文件)
  389. +----------------------------------------------------------
  390. * @access static
  391. +----------------------------------------------------------
  392. * @return void
  393. +----------------------------------------------------------
  394. */
  395. function delDir($directory, $subdir = true) {
  396. if (is_dir($directory) == false) {
  397. $this->error = "该目录是不存在!";
  398. return false;
  399. }
  400. $handle = opendir($directory);
  401. while (($file = readdir($handle)) !== false) {
  402. if ($file != "." && $file != "..") {
  403. is_dir("$directory/$file") ?
  404. Dir::delDir("$directory/$file") :
  405. unlink("$directory/$file");
  406. }
  407. }
  408. if (readdir($handle) == false) {
  409. closedir($handle);
  410. rmdir($directory);
  411. }
  412. }
  413. /**
  414. +----------------------------------------------------------
  415. * 删除目录下面的所有文件,但不删除目录
  416. +----------------------------------------------------------
  417. * @access static
  418. +----------------------------------------------------------
  419. * @return void
  420. +----------------------------------------------------------
  421. */
  422. function del($directory) {
  423. if (is_dir($directory) == false) {
  424. $this->error = "该目录是不存在!";
  425. return false;
  426. }
  427. $handle = opendir($directory);
  428. while (($file = readdir($handle)) !== false) {
  429. if ($file != "." && $file != ".." && is_file("$directory/$file")) {
  430. unlink("$directory/$file");
  431. }
  432. }
  433. closedir($handle);
  434. }
  435. /**
  436. +----------------------------------------------------------
  437. * 复制目录
  438. +----------------------------------------------------------
  439. * @access static
  440. +----------------------------------------------------------
  441. * @return void
  442. +----------------------------------------------------------
  443. */
  444. function copyDir($source, $destination) {
  445. if (is_dir($source) == false) {
  446. $this->error = "源目录不存在!";
  447. return false;
  448. }
  449. if (is_dir($destination) == false) {
  450. mkdir($destination, 0700);
  451. }
  452. $handle = opendir($source);
  453. while (false !== ($file = readdir($handle))) {
  454. if ($file != "." && $file != "..") {
  455. is_dir("$source/$file") ?
  456. Dir::copyDir("$source/$file", "$destination/$file") :
  457. copy("$source/$file", "$destination/$file");
  458. }
  459. }
  460. closedir($handle);
  461. }
  462. }