国内流行的内容管理系统(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.

205 lines
6.7KB

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