国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.5KB

  1. <?php
  2. if (!defined('DEDEINC')) exit ('dedebiz');
  3. /**
  4. * 文件管理逻辑类
  5. *
  6. * @version $id:file_class.php 19:09 2010年7月12日 tianya $
  7. * @package DedeBIZ.Administrator
  8. * @copyright Copyright (c) 2022 DedeBIZ.COM
  9. * @license GNU GPL v2 (https://www.dedebiz.com/license)
  10. * @link https://www.dedebiz.com
  11. */
  12. class FileManagement
  13. {
  14. var $baseDir = "";
  15. var $activeDir = "";
  16. //是否允许文件管理器删除目录,默认为不允许0,如果希望管理整个目录,请把值设为1
  17. var $allowDeleteDir = 0;
  18. //初始化系统
  19. function Init()
  20. {
  21. global $cfg_basedir, $activepath;
  22. $this->baseDir = $cfg_basedir;
  23. $this->activeDir = $activepath;
  24. }
  25. //修改文件名
  26. function RenameFile($oldname, $newname)
  27. {
  28. $oldname = $this->baseDir.$this->activeDir."/".$oldname;
  29. $newname = $this->baseDir.$this->activeDir."/".$newname;
  30. $oldext = pathinfo($oldname)['extension'];
  31. $newext = pathinfo($newname)['extension'];
  32. if ($oldext != $newext) {
  33. if (preg_match('#\.(php|pl|cgi|asp|aspx|jsp|php5|php4|php3|shtm|shtml)$#i', trim($newname))) {
  34. ShowMsg("文件扩展名已被系统禁止", "javascript:;");
  35. exit();
  36. }
  37. }
  38. if (($newname != $oldname) && is_writable($oldname)) {
  39. rename($oldname, $newname);
  40. }
  41. ShowMsg("成功修改一个文件名", "file_manage_main.php?activepath=".$this->activeDir);
  42. return 0;
  43. }
  44. //创建新目录
  45. function NewDir($dirname)
  46. {
  47. $newdir = $dirname;
  48. $dirname = $this->baseDir.$this->activeDir."/".$dirname;
  49. if (is_writable($this->baseDir.$this->activeDir)) {
  50. MkdirAll($dirname, $GLOBALS['cfg_dir_purview']);
  51. ShowMsg("成功创建一个新目录", "file_manage_main.php?activepath=".$this->activeDir."/".$newdir);
  52. return 1;
  53. } else {
  54. ShowMsg("创建新目录失败,因为这个位置不允许写入", "file_manage_main.php?activepath=".$this->activeDir);
  55. return 0;
  56. }
  57. }
  58. /**
  59. * 移动文件
  60. *
  61. * @access public
  62. * @param string $mfile 文件
  63. * @param string $mpath 路径
  64. * @return string
  65. */
  66. function MoveFile($mfile, $mpath)
  67. {
  68. if ($mpath != "" && !preg_match("#\.\.#", $mpath)) {
  69. $oldfile = $this->baseDir.$this->activeDir."/$mfile";
  70. $mpath = str_replace("\\", "/", $mpath);
  71. $mpath = preg_replace("#\/{1,}#", "/", $mpath);
  72. if (!preg_match("#^/#", $mpath)) {
  73. $mpath = $this->activeDir."/".$mpath;
  74. }
  75. $truepath = $this->baseDir.$mpath;
  76. if (is_readable($oldfile) && is_readable($truepath) && is_writable($truepath)) {
  77. if (is_dir($truepath)) {
  78. copy($oldfile, $truepath."/$mfile");
  79. } else {
  80. MkdirAll($truepath, $GLOBALS['cfg_dir_purview']);
  81. copy($oldfile, $truepath."/$mfile");
  82. }
  83. unlink($oldfile);
  84. ShowMsg("成功移动文件", "file_manage_main.php?activepath=$mpath", 0, 1000);
  85. return 1;
  86. } else {
  87. ShowMsg("移动文件<span class='text-primary'>$oldfile</span>&gt;<span class='text-primary'>$truepath/$mfile</span>失败,某个位置权限不足", "file_manage_main.php?activepath=$mpath", 0, 1000);
  88. return 0;
  89. }
  90. } else {
  91. ShowMsg("您移动的路径不合法", "-1", 0, 5000);
  92. return 0;
  93. }
  94. }
  95. /**
  96. * 删除目录
  97. *
  98. * @param string $indir
  99. */
  100. function RmDirFiles($indir)
  101. {
  102. if (!is_dir($indir)) {
  103. return;
  104. }
  105. $dh = dir($indir);
  106. while ($filename = $dh->read()) {
  107. if ($filename == "." || $filename == "..") {
  108. continue;
  109. } else if (is_file("$indir/$filename")) {
  110. @unlink("$indir/$filename");
  111. } else {
  112. $this->RmDirFiles("$indir/$filename");
  113. }
  114. }
  115. $dh->close();
  116. @rmdir($indir);
  117. }
  118. /**
  119. * 获得某目录合符规则的文件
  120. *
  121. * @param string $indir
  122. * @param string $fileexp
  123. * @param array $filearr
  124. */
  125. function GetMatchFiles($indir, $fileexp, &$filearr)
  126. {
  127. $dh = dir($indir);
  128. while ($filename = $dh->read()) {
  129. $truefile = $indir.'/'.$filename;
  130. if ($filename == "." || $filename == "..") {
  131. continue;
  132. } else if (is_dir($truefile)) {
  133. $this->GetMatchFiles($truefile, $fileexp, $filearr);
  134. } else if (substr($filename, -strlen($fileexp)) === $fileexp) {
  135. $filearr[] = $truefile;
  136. }
  137. }
  138. $dh->close();
  139. }
  140. /**
  141. * 删除文件
  142. *
  143. * @param string $filename
  144. * @return int
  145. */
  146. function DeleteFile($filename)
  147. {
  148. $filename = $this->baseDir.$this->activeDir."/$filename";
  149. if (is_file($filename)) {
  150. @unlink($filename);
  151. $t = "文件";
  152. } else {
  153. $t = "目录";
  154. if ($this->allowDeleteDir == 1) {
  155. $this->RmDirFiles($filename);
  156. } else {
  157. ShowMsg("系统禁止删除".$t."", "file_manage_main.php?activepath=".$this->activeDir);
  158. exit;
  159. }
  160. }
  161. ShowMsg("成功删除一个".$t."", "file_manage_main.php?activepath=".$this->activeDir);
  162. return 0;
  163. }
  164. }
  165. //目录文件大小检测类
  166. class SpaceUse
  167. {
  168. var $totalsize = 0;
  169. function checksize($indir)
  170. {
  171. $dh = dir($indir);
  172. while ($filename = $dh->read()) {
  173. if (!preg_match("#^\.#", $filename)) {
  174. if (is_dir("$indir/$filename")) {
  175. $this->checksize("$indir/$filename");
  176. } else {
  177. $this->totalsize = $this->totalsize + filesize("$indir/$filename");
  178. }
  179. }
  180. }
  181. }
  182. function setkb($size)
  183. {
  184. $size = $size / 1024;
  185. if ($size > 0) {
  186. list($t1, $t2) = explode(".", $size);
  187. $size = $t1.".".substr($t2, 0, 1);
  188. }
  189. return $size;
  190. }
  191. function setmb($size)
  192. {
  193. $size = $size / 1024 / 1024;
  194. if ($size > 0) {
  195. list($t1, $t2) = explode(".", $size);
  196. $size = $t1.".".substr($t2, 0, 2);
  197. }
  198. return $size;
  199. }
  200. }
  201. ?>