PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/php/idc_manager/cidc-read-only/library/Etao/File/Dir.php

http://timoseven.googlecode.com/
PHP | 474 lines | 336 code | 34 blank | 104 comment | 88 complexity | 8e80fa6b42c526ef24017d380985af34 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. * ?????
  4. */
  5. class Etao_File_Dir
  6. {
  7. /**
  8. * ??????listDirRecursive
  9. * @var array
  10. */
  11. public static $file_lists = array ();
  12. /**
  13. * ?????
  14. */
  15. public static $txt_files = array (
  16. 'txt', 'php', 'js', 'html', 'htm', 'css', 'phtml', 'xml'
  17. );
  18. /**
  19. * ????????????
  20. *
  21. * @param string $path
  22. * @param string $appendPath
  23. * @return array
  24. */
  25. public static function getDirectories ($path, $appendPath = false)
  26. {
  27. if (is_dir ($path)) {
  28. $contents = scandir ($path);
  29. if (is_array ($contents)) {
  30. $returnDirs = false;
  31. foreach ($contents as $dir)
  32. {
  33. if (is_dir ($path . '/' . $dir) && $dir != '.' && $dir != '..' && $dir != '.svn') {
  34. $returnDirs[] = $appendPath . $dir;
  35. }
  36. }
  37. if ($returnDirs) {
  38. return $returnDirs;
  39. }
  40. }
  41. }
  42. }
  43. /**
  44. * ???????/?????????
  45. *
  46. * @param string $file
  47. * @return string
  48. */
  49. public static function del_postfix ($file)
  50. {
  51. if (!preg_match ('#^/#', $file)) {
  52. throw new Exception ('?????/??');
  53. }
  54. $file = preg_replace ('#/$#', '', trim ($file));
  55. return $file;
  56. }
  57. /**
  58. * ????
  59. *
  60. * @param unknown_type $path
  61. */
  62. public static function make ($path)
  63. {
  64. return mkdir ($path, 0755);
  65. }
  66. /**
  67. * ??????
  68. * eg: /my/own/path
  69. * will create
  70. * >my
  71. * >>own
  72. * >>>path
  73. *
  74. * @param string $base
  75. * @param string $path
  76. */
  77. public static function makeRecursive ($base, $path)
  78. {
  79. $pathArray = explode ('/', $path);
  80. if (is_array ($pathArray)) {
  81. $strPath = null;
  82. foreach ($pathArray as $path)
  83. {
  84. if (!empty ($path)) {
  85. $strPath .= '/' . $path;
  86. if (!is_dir ($base . $strPath)) {
  87. if (!self::make ($base . $strPath)) {
  88. return false;
  89. }
  90. }
  91. }
  92. }
  93. return true;
  94. }
  95. }
  96. /**
  97. * ????????
  98. *
  99. * @param string $source
  100. * @param string $newName
  101. */
  102. public static function rename ($source, $newName)
  103. {
  104. if (is_dir ($source)) {
  105. return rename ($source, $newName);
  106. }
  107. }
  108. /**
  109. * ?????
  110. * @param string $source
  111. * @param string $target
  112. */
  113. public static function copyRecursive ($source, $target)
  114. {
  115. if (is_dir ($source)) {
  116. @mkdir ($target);
  117. $d = dir ($source);
  118. while (false !== ($entry = $d->read ()))
  119. {
  120. if ($entry == '.' || $entry == '..') {
  121. continue;
  122. }
  123. $Entry = $source . '/' . $entry;
  124. if (is_dir ($Entry)) {
  125. Etao_File_Directory_Writer::copyRecursive ($Entry, $target . '/' . $entry);
  126. continue;
  127. }
  128. copy ($Entry, $target . '/' . $entry);
  129. }
  130. $d->close ();
  131. } else {
  132. copy ($source, $target);
  133. }
  134. }
  135. /**
  136. * ?????
  137. *
  138. * @param string $target
  139. * @param bool $verbose
  140. * @return bool
  141. */
  142. public static function deleteRecursive ($target, $verbose=false)
  143. {
  144. $exceptions = array ('.', '..');
  145. if (!$sourcedir = @opendir ($target)) {
  146. if ($verbose) {
  147. echo '<strong>Couldn&#146;t open ' . $target . "</strong><br />\n";
  148. }
  149. return false;
  150. }
  151. while (false !== ($sibling = readdir ($sourcedir)))
  152. {
  153. if (!in_array ($sibling, $exceptions)) {
  154. $object = str_replace ('//', '/', $target . '/' . $sibling);
  155. if ($verbose)
  156. echo 'Processing: <strong>' . $object . "</strong><br />\n";
  157. if (is_dir ($object))
  158. Etao_File_Dir::deleteRecursive ($object);
  159. if (is_file ($object)) {
  160. $result = @unlink ($object);
  161. if ($verbose && $result)
  162. echo "File has been removed<br />\n";
  163. if ($verbose && (!$result))
  164. echo "<strong>Couldn&#146;t remove file</strong>";
  165. }
  166. }
  167. }
  168. closedir ($sourcedir);
  169. if ($result = @rmdir ($target)) {
  170. if ($verbose) {
  171. echo "Target directory has been removed<br />\n";
  172. return true;
  173. }
  174. } else {
  175. if ($verbose) {
  176. echo "<strong>Couldn&#146;t remove target directory</strong>";
  177. return false;
  178. }
  179. }
  180. }
  181. /**
  182. * ???????
  183. * @param string $file
  184. * @param bool $dot ????
  185. */
  186. public static function getSuffix ($file, $dot=false)
  187. {
  188. if ($file === null)
  189. return false;
  190. $suffix = strtolower (pathinfo ($file, PATHINFO_EXTENSION));
  191. if ($dot) {
  192. $suffix = '.' . $suffix;
  193. }
  194. return $suffix;
  195. }
  196. /**
  197. * ????????????????
  198. * @param $path
  199. * @param $pattern
  200. * @param $mode
  201. * @param $deep
  202. */
  203. public static function findFile ($path, $pattern, $mode = 'filename', $deep = 0)
  204. {
  205. if (!is_dir ($path))
  206. return false;
  207. $path = self::pathReplace ($path);
  208. $alldata = glob ($path . '*');
  209. if (!is_array ($alldata))
  210. return array ();
  211. if ($mode == 'filename') {
  212. $finds = preg_grep ($pattern, $alldata);
  213. } elseif ($mode == 'filedata') {
  214. foreach ($alldata as $file)
  215. {
  216. if (is_file ($file))
  217. $data[$file] = file_get_contents ($file);
  218. }
  219. $finds = array_keys (preg_grep ($pattern, $data));
  220. }
  221. if ($deep) {
  222. foreach ($alldata as $file)
  223. {
  224. if (is_dir ($file))
  225. $finds = array_merge ($finds, self::findFile ($file, $pattern, $mode, $deep));
  226. }
  227. }
  228. return $finds;
  229. }
  230. /**
  231. * ???????????
  232. * @param unknown_type $path
  233. * @param unknown_type $type
  234. * @param unknown_type $array
  235. */
  236. public static function listFile ($path, $type = null, & $array = array ())
  237. {
  238. if (!is_dir ($path))
  239. return false;
  240. $path = self::pathReplace ($path);
  241. $alldata = glob ($path . '*');
  242. if (!is_array ($alldata))
  243. return $array;
  244. switch ($type)
  245. {
  246. case 'file':
  247. foreach ($alldata as $data)
  248. {
  249. if (is_dir ($data)) {
  250. self::listFile ($data, $type, & $array);
  251. } else {
  252. $array[] = $data;
  253. }
  254. }
  255. break;
  256. case 'dir':
  257. foreach ($alldata as $data)
  258. {
  259. if (is_dir ($data)) {
  260. $array[] = $data;
  261. self::listFile ($data, $type, & $array);
  262. }
  263. }
  264. break;
  265. default:
  266. foreach ($alldata as $data)
  267. {
  268. if (is_dir ($data)) {
  269. $array['dir'][] = $data;
  270. self::listFile ($data, $type, & $array);
  271. } else {
  272. $array['file'][] = $data;
  273. }
  274. }
  275. break;
  276. }
  277. return $array;
  278. }
  279. /**
  280. * ??????
  281. * @param $path
  282. */
  283. public static function sizeFile ($path)
  284. {
  285. if (!is_dir ($path))
  286. return false;
  287. $size = 0;
  288. $path = self::pathReplace ($path);
  289. $alldata = glob ($path . '*');
  290. if (!is_array ($alldata))
  291. return $size;
  292. foreach ($alldata as $data)
  293. {
  294. if (is_dir ($data)) {
  295. $size += self::sizeFile ($data);
  296. } else {
  297. $size += filesize ($data);
  298. }
  299. }
  300. return $size;
  301. }
  302. public static function randMd5 ($value=null)
  303. {
  304. if ($value === null) {
  305. $value = md5 (time () . rand (2, 1048576));
  306. }
  307. $dir = substr ($value, 8, 2) . '/' . substr ($value, 23, 2) . '/';
  308. return $dir;
  309. }
  310. /**
  311. * ??????
  312. * @param unknown_type $filesize
  313. */
  314. public static function sizeUnit ($filesize)
  315. {
  316. if ($filesize >= 1073741824) {
  317. $filesize = round ($filesize / 1073741824 * 100) / 100 . ' GB';
  318. } elseif ($filesize >= 1048576) {
  319. $filesize = round ($filesize / 1048576 * 100) / 100 . ' MB';
  320. } elseif ($filesize >= 1024) {
  321. $filesize = round ($filesize / 1024 * 100) / 100 . ' KB';
  322. } else {
  323. $filesize = $filesize . ' Bytes';
  324. }
  325. return $filesize;
  326. }
  327. public static function pathReplace ($path)
  328. {
  329. return rtrim (preg_replace ("|[\/]+|", DS, $path), DS) . DS;
  330. }
  331. /**
  332. * ??????????????
  333. */
  334. public static function printFileInPath ($path)
  335. {
  336. $files = self::listFile ($path);
  337. $html = '';
  338. if (key_exists ('file', $files)) {
  339. foreach ($files['file'] as $filename)
  340. {
  341. if (in_array (self::getSuffix ($filename), self::$txt_files)) {
  342. $html .= self::printFile ($filename);
  343. } else {
  344. $html .= '<strong><a name="' . $filename . '">' . $filename . '</a> (' . self::getSuffix ($filename) . '????????)</strong><br />';
  345. }
  346. }
  347. return $html;
  348. }
  349. }
  350. /**
  351. * ???????
  352. */
  353. public static function printFile ($filename)
  354. {
  355. $html = '';
  356. if (file_exists ($filename) && is_readable ($filename)) {
  357. if (in_array (self::getSuffix ($filename), self::$txt_files)) {
  358. ob_start (); //?????
  359. $html .= '<strong><a name="' . $filename . '">' . $filename . '</a></strong><br />';
  360. $html .='<blockquote>';
  361. highlight_file ($filename);
  362. $html .= ob_get_contents (); //?????????????$info
  363. $html .='</blockquote>';
  364. ob_clean ();
  365. }
  366. }
  367. return $html;
  368. }
  369. /**
  370. * ?????????
  371. * @param string $dir
  372. */
  373. public static function isEmptyDir ($dir)
  374. {
  375. $result = TRUE;
  376. $d = dir ($dir);
  377. while (($entry = $d->read ()) !== false)
  378. {
  379. if ($entry !== '.' && $entry != '..') {
  380. $result = FALSE;
  381. break;
  382. }
  383. }
  384. $d->close ();
  385. return $result;
  386. }
  387. /* ?? listDirRecursive( $dirName = null )
  388. * * ?? ?????????????
  389. * * ?? $dirName ????
  390. * * ?? ?????? false???
  391. */
  392. public static function listDirRecursive ($dirName = null)
  393. {
  394. if (empty ($dirName)) {
  395. throw new Exception ("directory is empty.");
  396. } else {
  397. $dirName = self::del_postfix ($dirName);
  398. }
  399. if (is_dir ($dirName)) {
  400. if ($dh = opendir ($dirName)) {
  401. $tree = array ();
  402. while (($file = readdir ($dh)) !== false)
  403. {
  404. if ($file != "." && $file != "..") {
  405. $filePath = $dirName . "/" . $file;
  406. //self::$file_lists[] = $filePath;
  407. if (is_dir ($filePath)) {//???,??
  408. self::$file_lists[] = '[' . $filePath . ']';
  409. $tree[$file] = self::listDirRecursive ($filePath);
  410. } else {//???,???????
  411. self::$file_lists[] = $filePath;
  412. $tree[] = $file;
  413. }
  414. }
  415. }
  416. closedir ($dh);
  417. } else {
  418. throw new Exception ("can not open directory $dirName.");
  419. }
  420. return self::$file_lists;
  421. } else {
  422. throw new Exception ("$dirName is not a directory.");
  423. }
  424. }
  425. /**
  426. *??????
  427. * @param type $path
  428. * @param type $time
  429. */
  430. public static function deleteOldFile ($path, $time=60)
  431. {
  432. $iterator = new DirectoryIterator ($path);
  433. foreach ($iterator as $fileinfo)
  434. {
  435. if ((time () - $fileinfo->getCTime ()) > $time) {
  436. @unlink ($path . DIRECTORY_SEPARATOR . $fileinfo->getFilename ());
  437. }
  438. }
  439. }
  440. }