PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/code/addons/libs/Io/Dir.class.php

http://thinksns-2.googlecode.com/
PHP | 523 lines | 231 code | 33 blank | 259 comment | 36 complexity | 015ec00a29063cee2135a2220a61d5d7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * DirectoryIterator??? PHP5?????DirectoryIterator?
  15. +------------------------------------------------------------------------------
  16. * @category ORG
  17. * @package ORG
  18. * @subpackage Io
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. class Dir implements IteratorAggregate
  24. {//?????
  25. private $_values = array();
  26. /**
  27. +----------------------------------------------------------
  28. * ????
  29. +----------------------------------------------------------
  30. * @access public
  31. +----------------------------------------------------------
  32. * @param string $path ????
  33. +----------------------------------------------------------
  34. */
  35. function __construct($path,$pattern='*')
  36. {
  37. if(substr($path, -1) != "/") $path .= "/";
  38. $this->listFile($path,$pattern);
  39. }
  40. /**
  41. +----------------------------------------------------------
  42. * ???????????
  43. +----------------------------------------------------------
  44. * @access public
  45. +----------------------------------------------------------
  46. * @param mixed $pathname ??
  47. +----------------------------------------------------------
  48. */
  49. function listFile($pathname,$pattern='*')
  50. {
  51. static $_listDirs = array();
  52. $guid = md5($pathname.$pattern);
  53. if(!isset($_listDirs[$guid])){
  54. $dir = array();
  55. $list = glob($pathname.$pattern);
  56. foreach ($list as $i=>$file){
  57. $dir[$i]['filename'] = basename($file);
  58. $dir[$i]['pathname'] = realpath($file);
  59. $dir[$i]['owner'] = fileowner($file);
  60. $dir[$i]['perms'] = fileperms($file);
  61. $dir[$i]['inode'] = fileinode($file);
  62. $dir[$i]['group'] = filegroup($file);
  63. $dir[$i]['path'] = dirname($file);
  64. $dir[$i]['atime'] = fileatime($file);
  65. $dir[$i]['ctime'] = filectime($file);
  66. $dir[$i]['size'] = filesize($file);
  67. $dir[$i]['type'] = filetype($file);
  68. $dir[$i]['ext'] = is_file($file)?strtolower(substr(strrchr(basename($file), '.'),1)):'';
  69. $dir[$i]['mtime'] = filemtime($file);
  70. $dir[$i]['isDir'] = is_dir($file);
  71. $dir[$i]['isFile'] = is_file($file);
  72. $dir[$i]['isLink'] = is_link($file);
  73. //$dir[$i]['isExecutable']= function_exists('is_executable')?is_executable($file):'';
  74. $dir[$i]['isReadable'] = is_readable($file);
  75. $dir[$i]['isWritable'] = is_writable($file);
  76. }
  77. $cmp_func = create_function('$a,$b','
  78. $k = "isDir";
  79. if($a[$k] == $b[$k]) return 0;
  80. return $a[$k]>$b[$k]?-1:1;
  81. ');
  82. // ????? ???????
  83. usort($dir,$cmp_func);
  84. $this->_values = $dir;
  85. $_listDirs[$guid] = $dir;
  86. }else{
  87. $this->_values = $_listDirs[$guid];
  88. }
  89. }
  90. /**
  91. +----------------------------------------------------------
  92. * ????????
  93. +----------------------------------------------------------
  94. * @access public
  95. +----------------------------------------------------------
  96. * @return integer
  97. +----------------------------------------------------------
  98. */
  99. function getATime()
  100. {
  101. $current = $this->current($this->_values);
  102. return $current['atime'];
  103. }
  104. /**
  105. +----------------------------------------------------------
  106. * ????? inode ????
  107. +----------------------------------------------------------
  108. * @access public
  109. +----------------------------------------------------------
  110. * @return integer
  111. +----------------------------------------------------------
  112. */
  113. function getCTime()
  114. {
  115. $current = $this->current($this->_values);
  116. return $current['ctime'];
  117. }
  118. /**
  119. +----------------------------------------------------------
  120. * ?????????
  121. +----------------------------------------------------------
  122. * @access public
  123. +----------------------------------------------------------
  124. * @return DirectoryIterator
  125. +----------------------------------------------------------
  126. */
  127. function getChildren()
  128. {
  129. $current = $this->current($this->_values);
  130. if($current['isDir']){
  131. return new Dir($current['pathname']);
  132. }
  133. return false;
  134. }
  135. /**
  136. +----------------------------------------------------------
  137. * ?????
  138. +----------------------------------------------------------
  139. * @access public
  140. +----------------------------------------------------------
  141. * @return string
  142. +----------------------------------------------------------
  143. */
  144. function getFilename()
  145. {
  146. $current = $this->current($this->_values);
  147. return $current['filename'];
  148. }
  149. /**
  150. +----------------------------------------------------------
  151. * ??????
  152. +----------------------------------------------------------
  153. * @access public
  154. +----------------------------------------------------------
  155. * @return integer
  156. +----------------------------------------------------------
  157. */
  158. function getGroup()
  159. {
  160. $current = $this->current($this->_values);
  161. return $current['group'];
  162. }
  163. /**
  164. +----------------------------------------------------------
  165. * ????? inode
  166. +----------------------------------------------------------
  167. * @access public
  168. +----------------------------------------------------------
  169. * @return integer
  170. +----------------------------------------------------------
  171. */
  172. function getInode()
  173. {
  174. $current = $this->current($this->_values);
  175. return $current['inode'];
  176. }
  177. /**
  178. +----------------------------------------------------------
  179. * ???????????
  180. +----------------------------------------------------------
  181. * @access public
  182. +----------------------------------------------------------
  183. * @return integer
  184. +----------------------------------------------------------
  185. */
  186. function getMTime()
  187. {
  188. $current = $this->current($this->_values);
  189. return $current['mtime'];
  190. }
  191. /**
  192. +----------------------------------------------------------
  193. * ????????
  194. +----------------------------------------------------------
  195. * @access public
  196. +----------------------------------------------------------
  197. * @return string
  198. +----------------------------------------------------------
  199. */
  200. function getOwner()
  201. {
  202. $current = $this->current($this->_values);
  203. return $current['owner'];
  204. }
  205. /**
  206. +----------------------------------------------------------
  207. * ?????????????
  208. +----------------------------------------------------------
  209. * @access public
  210. +----------------------------------------------------------
  211. * @return string
  212. +----------------------------------------------------------
  213. */
  214. function getPath()
  215. {
  216. $current = $this->current($this->_values);
  217. return $current['path'];
  218. }
  219. /**
  220. +----------------------------------------------------------
  221. * ???????????????
  222. +----------------------------------------------------------
  223. * @access public
  224. +----------------------------------------------------------
  225. * @return string
  226. +----------------------------------------------------------
  227. */
  228. function getPathname()
  229. {
  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. {
  244. $current = $this->current($this->_values);
  245. return $current['perms'];
  246. }
  247. /**
  248. +----------------------------------------------------------
  249. * ???????
  250. +----------------------------------------------------------
  251. * @access public
  252. +----------------------------------------------------------
  253. * @return integer
  254. +----------------------------------------------------------
  255. */
  256. function getSize()
  257. {
  258. $current = $this->current($this->_values);
  259. return $current['size'];
  260. }
  261. /**
  262. +----------------------------------------------------------
  263. * ??????
  264. +----------------------------------------------------------
  265. * @access public
  266. +----------------------------------------------------------
  267. * @return string
  268. +----------------------------------------------------------
  269. */
  270. function getType()
  271. {
  272. $current = $this->current($this->_values);
  273. return $current['type'];
  274. }
  275. /**
  276. +----------------------------------------------------------
  277. * ?????
  278. +----------------------------------------------------------
  279. * @access public
  280. +----------------------------------------------------------
  281. * @return boolen
  282. +----------------------------------------------------------
  283. */
  284. function isDir()
  285. {
  286. $current = $this->current($this->_values);
  287. return $current['isDir'];
  288. }
  289. /**
  290. +----------------------------------------------------------
  291. * ?????
  292. +----------------------------------------------------------
  293. * @access public
  294. +----------------------------------------------------------
  295. * @return boolen
  296. +----------------------------------------------------------
  297. */
  298. function isFile()
  299. {
  300. $current = $this->current($this->_values);
  301. return $current['isFile'];
  302. }
  303. /**
  304. +----------------------------------------------------------
  305. * ???????????
  306. +----------------------------------------------------------
  307. * @access public
  308. +----------------------------------------------------------
  309. * @return boolen
  310. +----------------------------------------------------------
  311. */
  312. function isLink()
  313. {
  314. $current = $this->current($this->_values);
  315. return $current['isLink'];
  316. }
  317. /**
  318. +----------------------------------------------------------
  319. * ????????
  320. +----------------------------------------------------------
  321. * @access public
  322. +----------------------------------------------------------
  323. * @return boolen
  324. +----------------------------------------------------------
  325. */
  326. function isExecutable()
  327. {
  328. $current = $this->current($this->_values);
  329. return $current['isExecutable'];
  330. }
  331. /**
  332. +----------------------------------------------------------
  333. * ??????
  334. +----------------------------------------------------------
  335. * @access public
  336. +----------------------------------------------------------
  337. * @return boolen
  338. +----------------------------------------------------------
  339. */
  340. function isReadable()
  341. {
  342. $current = $this->current($this->_values);
  343. return $current['isReadable'];
  344. }
  345. /**
  346. +----------------------------------------------------------
  347. * ??foreach?????
  348. +----------------------------------------------------------
  349. * @access public
  350. +----------------------------------------------------------
  351. * @return string
  352. +----------------------------------------------------------
  353. */
  354. function getIterator()
  355. {
  356. return new ArrayObject($this->_values);
  357. }
  358. // ?????????
  359. function toArray() {
  360. return $this->_values;
  361. }
  362. // ????
  363. /**
  364. +----------------------------------------------------------
  365. * ????????
  366. +----------------------------------------------------------
  367. * @access static
  368. +----------------------------------------------------------
  369. * @return void
  370. +----------------------------------------------------------
  371. */
  372. function isEmpty($directory)
  373. {
  374. $handle = opendir($directory);
  375. while (($file = readdir($handle)) !== false)
  376. {
  377. if ($file != "." && $file != "..")
  378. {
  379. closedir($handle);
  380. return false;
  381. }
  382. }
  383. closedir($handle);
  384. return true;
  385. }
  386. /**
  387. +----------------------------------------------------------
  388. * ??????????
  389. +----------------------------------------------------------
  390. * @access static
  391. +----------------------------------------------------------
  392. * @return void
  393. +----------------------------------------------------------
  394. */
  395. function getList($directory)
  396. {
  397. return scandir($directory);
  398. }
  399. /**
  400. +----------------------------------------------------------
  401. * ?????????????
  402. +----------------------------------------------------------
  403. * @access static
  404. +----------------------------------------------------------
  405. * @return void
  406. +----------------------------------------------------------
  407. */
  408. function delDir($directory,$subdir=true)
  409. {
  410. if (is_dir($directory) == false)
  411. {
  412. exit("The Directory Is Not Exist!");
  413. }
  414. $handle = opendir($directory);
  415. while (($file = readdir($handle)) !== false)
  416. {
  417. if ($file != "." && $file != "..")
  418. {
  419. is_dir("$directory/$file")?
  420. Dir::delDir("$directory/$file"):
  421. unlink("$directory/$file");
  422. }
  423. }
  424. if (readdir($handle) == false)
  425. {
  426. closedir($handle);
  427. rmdir($directory);
  428. }
  429. }
  430. /**
  431. +----------------------------------------------------------
  432. * ??????????????????
  433. +----------------------------------------------------------
  434. * @access static
  435. +----------------------------------------------------------
  436. * @return void
  437. +----------------------------------------------------------
  438. */
  439. function del($directory)
  440. {
  441. if (is_dir($directory) == false)
  442. {
  443. exit("The Directory Is Not Exist!");
  444. }
  445. $handle = opendir($directory);
  446. while (($file = readdir($handle)) !== false)
  447. {
  448. if ($file != "." && $file != ".." && is_file("$directory/$file"))
  449. {
  450. unlink("$directory/$file");
  451. }
  452. }
  453. closedir($handle);
  454. }
  455. /**
  456. +----------------------------------------------------------
  457. * ????
  458. +----------------------------------------------------------
  459. * @access static
  460. +----------------------------------------------------------
  461. * @return void
  462. +----------------------------------------------------------
  463. */
  464. function copyDir($source, $destination)
  465. {
  466. if (is_dir($source) == false)
  467. {
  468. exit("The Source Directory Is Not Exist!");
  469. }
  470. if (is_dir($destination) == false)
  471. {
  472. mkdir($destination, 0700);
  473. }
  474. $handle=opendir($source);
  475. while (false !== ($file = readdir($handle)))
  476. {
  477. if ($file != "." && $file != "..")
  478. {
  479. is_dir("$source/$file")?
  480. Dir::copyDir("$source/$file", "$destination/$file"):
  481. copy("$source/$file", "$destination/$file");
  482. }
  483. }
  484. closedir($handle);
  485. }
  486. }//?????
  487. if(!class_exists('DirectoryIterator')) {
  488. class DirectoryIterator extends Dir {}
  489. }
  490. ?>