common.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. // 应用公共文件
  3. /**
  4. * 数据库查询结果集无限级分类
  5. * @param mixed $list 结果集
  6. * @param string $pk 主键
  7. * @param string $pid 父级字段名
  8. * @param string $child 子结果集
  9. * @param int $root 起始id
  10. * @return @return array
  11. */
  12. function list_tree($list, $pk = 'id', $pid = 'pid', $child = 'child', $root = 0)
  13. {
  14. $tree = [];
  15. foreach ($list as $key => $val) {
  16. if ($val[$pid] == $root) {
  17. unset($list[$key]);
  18. if (!empty($list)) {
  19. $children = list_tree($list, $pk, $pid, $child, $val[$pk]);
  20. $val[$child] = $children;
  21. }
  22. array_push($tree, $val);
  23. }
  24. }
  25. return $tree;
  26. }
  27. function make_tree($list, $pk = 'id', $pid = 'pid', $child = 'child', $root = 0)
  28. {
  29. $tree=array();
  30. foreach ($list as $key => $val) {
  31. if ($val[$pid]==$root) {
  32. unset($list[$key]);
  33. if (!empty($list)) {
  34. $child = make_tree($list, $pk, $pid, $child, $val[$pk]);
  35. if (!empty($child)) {
  36. $val['child']=$child;
  37. } else {
  38. $val['child']= array();
  39. }
  40. }
  41. $tree[]=$val;
  42. }
  43. }
  44. return $tree;
  45. }
  46. /**
  47. * 获取指定月份的第一天开始和最后一天结束的时间戳
  48. *
  49. * @param int $y 年份 $m 月份
  50. * @return array(本月开始时间,本月结束时间)
  51. */
  52. function month_frist_and_last_day($y = "", $m = ""){
  53. if ($y == "") $y = date("Y");
  54. if ($m == "") $m = date("m");
  55. $m = sprintf("%02d", intval($m));
  56. $y = str_pad(intval($y), 4, "0", STR_PAD_RIGHT);
  57. $m>12 || $m<1 ? $m=1 : $m=$m;
  58. $firstday = strtotime($y . $m . "01000000");
  59. $firstdaystr = date("Y-m-01", $firstday);
  60. $lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
  61. return [ "firstday" => $firstday, "lastday" => $lastday];
  62. }
  63. /**
  64. * 生成随机字符串
  65. */
  66. function generate_stochastic_string($length = 16)
  67. {
  68. $permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyz';
  69. return substr(str_shuffle($permitted_chars), 0, $length);
  70. }
  71. /**
  72. * PHP格式化字节大小
  73. * @param number $size 字节数
  74. * @param string $delimiter 数字和单位分隔符
  75. * @return string 格式化后的带单位的大小
  76. */
  77. function format_bytes(int $size, string $delimiter = ''): string
  78. {
  79. $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  80. for ($i = 0; $size >= 1024 && $i < 5; $i++) {
  81. $size = (int) $size / 1024;
  82. }
  83. return round($size, 2) . $delimiter . $units[$i];
  84. }
  85. /**
  86. * 使用递归遍历获取文件夹的大小
  87. */
  88. function get_dir_size($dirname)
  89. {
  90. static $tot; //这里把$tot定义为静态的,表示$tot全局只有这一个变量
  91. $ds = opendir($dirname); //创建一个目录资源, 传入的目录就是资源
  92. while ($file = readdir($ds)) { //从目录中读取到条目
  93. //这里的$path 表示这个路径下的文件夹,如果不这么去定义,里边执行递归语句的时候,找不到是那个文件夹
  94. $path = $dirname . "/" . $file;
  95. //判断,如果是 . 或者 ..的目录就过滤掉
  96. if ($file != "." && $file != "..") {
  97. if (is_dir($path)) { //判断如果找到的是目录
  98. get_dir_size($path); //如果得到是文件夹,然后递归调用一次方法传入的$path文件夹路径就是判断得到的文件夹赋值给$dirname
  99. } else {
  100. $tot += filesize($path);
  101. }
  102. }
  103. }
  104. return $tot;
  105. }
  106. /**
  107. * 递归删除文件夹
  108. */
  109. function deldir($path)
  110. {
  111. //如果是目录则继续
  112. if (is_dir($path)) {
  113. //扫描一个文件夹内的所有文件夹和文件并返回数组
  114. $p = scandir($path);
  115. foreach ($p as $val) {
  116. //排除目录中的.和..
  117. if ($val != "." && $val != "..") {
  118. //如果是目录则递归子目录,继续操作
  119. if (is_dir($path . $val)) {
  120. //子目录中操作删除文件夹和文件
  121. deldir($path . $val . DIRECTORY_SEPARATOR);
  122. //目录清空后删除空文件夹
  123. @rmdir($path . $val . DIRECTORY_SEPARATOR);
  124. } else {
  125. //如果是文件直接删除
  126. unlink($path . $val);
  127. }
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * 遍历获取目录下的指定类型的文件
  134. * @param $path
  135. * @param array $files
  136. * @return array
  137. */
  138. function getfiles($path, $allowFiles, &$files = array())
  139. {
  140. if (!is_dir($path)) return null;
  141. if (substr($path, strlen($path) - 1) != '/') $path .= '/';
  142. $handle = opendir($path);
  143. while (false !== ($file = readdir($handle))) {
  144. if ($file != '.' && $file != '..') {
  145. $path2 = $path . $file;
  146. if (is_dir($path2)) {
  147. getfiles($path2, $allowFiles, $files);
  148. } else {
  149. if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
  150. $files[] = array(
  151. 'url' => substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
  152. 'mtime' => filemtime($path2)
  153. );
  154. }
  155. }
  156. }
  157. }
  158. return $files;
  159. }