Dir.php 18 KB

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