tools.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace helper;
  3. class tools
  4. {
  5. /**
  6. * 将数组生成树结构
  7. * @param Array $items
  8. * @param String $id
  9. * @param String $pid
  10. * @param String $son
  11. * @return Array
  12. */
  13. public static function tree($items, $id = 'cid', $pid = 'pid', $son = 'children')
  14. {
  15. $tree = array(); //格式化的树
  16. $tmpMap = array(); //临时扁平数据
  17. foreach ($items as $item) {
  18. $tmpMap[$item[$id]] = $item;
  19. }
  20. foreach ($items as $item) {
  21. if ($item[$id] != $item[$pid] && isset($tmpMap[$item[$pid]])) {
  22. $tmpMap[$item[$pid]][$son][$item[$id]] = &$tmpMap[$item[$id]];
  23. } else {
  24. $tree[$item[$id]] = &$tmpMap[$item[$id]];
  25. }
  26. }
  27. return $tree;
  28. }
  29. /**
  30. * 格式化文件大小
  31. */
  32. public static function formatSize($bytes, $unit = "", $decimals = 2, $showUnit = true)
  33. {
  34. $units = array('B' => 0, 'KB' => 1, 'MB' => 2, 'GB' => 3, 'TB' => 4, 'PB' => 5, 'EB' => 6, 'ZB' => 7, 'YB' => 8);
  35. $bytes = (int)$bytes;
  36. $value = 0;
  37. if ($bytes > 0) {
  38. if (!array_key_exists($unit, $units)) {
  39. $pow = floor(log($bytes) / log(1024));
  40. $unit = array_search($pow, $units);
  41. }
  42. $value = (intval($bytes) / pow(1024, floor($units[$unit])));
  43. }
  44. if (!is_numeric($decimals) || $decimals < 0) {
  45. $decimals = 2;
  46. }
  47. if ($unit == 'KB' && $value < 0.1) {
  48. $decimals = 0;
  49. }
  50. if ($showUnit) {
  51. return sprintf('%.' . $decimals . 'f' . $unit, $value);
  52. } else {
  53. return sprintf('%.' . $decimals . 'f ', $value);
  54. }
  55. }
  56. /**
  57. * 获取用户访问目录路径
  58. * @param string $path 目录
  59. */
  60. public static function getVisitPath($path)
  61. {
  62. $usePath = explode('/', $path);
  63. $dirPathes = array();
  64. $lastDir = $usePath[0];
  65. foreach ($usePath AS $key => $path) {
  66. if (!$path) continue;
  67. $array = array();
  68. $array['name'] = $path;
  69. if ($key > 0) {
  70. $lastDir = $lastDir . '/' . $path;
  71. }
  72. $array['path'] = $lastDir;
  73. $array['url'] = _link('/dirs?path=' . urlencode($lastDir));
  74. $dirPathes[] = $array;
  75. }
  76. return $dirPathes;
  77. }
  78. public static function fileType($fullPath)
  79. {
  80. if (is_dir($fullPath)) return 'folder';
  81. return pathinfo($fullPath, PATHINFO_EXTENSION);
  82. }
  83. /**
  84. * 是否是图片文件
  85. * @param string $fullPath 文件路径
  86. * @return bool
  87. */
  88. public static function isImage($fullPath)
  89. {
  90. $fileType = self::fileType($fullPath);
  91. if (in_array($fileType, array('gif', 'jpg', 'jpeg', 'webp', 'png', 'bmp'))) {
  92. return true;
  93. }
  94. return false;
  95. }
  96. /**
  97. * 获取目录中文件及目录
  98. * @param string $path 目录
  99. * @return array
  100. */
  101. public static function getFolders($path, $filter = '*')
  102. {
  103. $dir = dir($path);
  104. $filetype = _include('../private/configure/filetype.config.php');
  105. $files = array();
  106. while (($file = $dir->read()) !== false) {
  107. if ($file == '.' || $file == '..') continue;
  108. $fullPath = str_replace('//', '/', $path . '/' . $file);
  109. $isDir = is_dir($fullPath);
  110. if ($filter != '*' && !$isDir && !preg_match('/' . $filter . '$/', $file)) continue;
  111. $array = array();
  112. $type = self::fileType($fullPath);
  113. $array['name'] = iconv('GBK', 'UTF-8', $file);
  114. if (!in_array($type, $filetype)) {
  115. $array['icon'] = _link('filetype/unknow.png');
  116. } else {
  117. $array['icon'] = _link('filetype/' . $type . '.gif');
  118. }
  119. $array['type'] = is_dir($fullPath) ? 'folder' : 'file';
  120. $array['url'] = $array['type'] == 'folder' ? '/dirs?path=' . urlencode($fullPath) : '/dirs/file?file=' . urlencode($fullPath);
  121. $array['remove'] = _link('/dirs/remove?file=' . $fullPath . '&isAjax=1');
  122. $array['path'] = _link($fullPath);
  123. $size = $array['type'] == 'folder' ? '' : filesize($fullPath);
  124. $array['size'] = $array['type'] == 'folder' ? '' : \helper\tools::formatSize($size, 'KB');
  125. $array['isImage'] = self::isImage($fullPath);
  126. //如果文件超过200k就不让直接查看,让下载后查看
  127. if ($size > 1024 * 200) {
  128. $array['url'] = '/dirs/down?file=' . urlencode($fullPath);
  129. }
  130. $array['url'] = _link($array['url']);
  131. $array['extension'] = $type;
  132. $array['createAt'] = filectime($fullPath);
  133. $array['updateAt'] = fileatime($fullPath);
  134. $files[] = $array;
  135. }
  136. $dir->close();
  137. return $files;
  138. }
  139. /**
  140. * 不在指定目录的文件不让删除
  141. *
  142. * @param $file
  143. * @return bool
  144. */
  145. public static function allowRemove($file)
  146. {
  147. $allowFolder = array('tmp', 'tmp/compile');
  148. $file = ltrim($file, './');
  149. foreach ($allowFolder AS $allow) {
  150. if (stristr($file, $allow)) return true;
  151. }
  152. return false;
  153. }
  154. /**
  155. * 删除指定文件夹或文件
  156. * @param $dir
  157. * @return bool
  158. */
  159. public static function removeFile($dir)
  160. {
  161. //只让删除指定文件夹的文件,其他文件夹中的不让删除
  162. if (!self::allowRemove($dir)) {
  163. return false;
  164. }
  165. if (is_file($dir)) return unlink($dir);
  166. $dh = opendir($dir);
  167. while ($file = readdir($dh)) {
  168. if ($file != "." && $file != "..") {
  169. $fullpath = $dir . "/" . $file;
  170. if (!is_dir($fullpath)) {
  171. unlink($fullpath);
  172. } else {
  173. self::removeFile($fullpath);
  174. }
  175. }
  176. }
  177. closedir($dh);
  178. //删除当前文件夹:
  179. if (rmdir($dir)) {
  180. return true;
  181. } else {
  182. return false;
  183. }
  184. }
  185. }