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

209 lines
6.5KB

  1. <?php if (!defined('DEDEINC')) exit('dedebiz');
  2. /**
  3. * 文件管理逻辑类
  4. *
  5. * @version $Id: file_class.php 1 19:09 2010年7月12日Z tianya $
  6. * @package DedeBIZ.Administrator
  7. * @copyright Copyright (c) 2020, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. class FileManagement
  12. {
  13. var $baseDir = "";
  14. var $activeDir = "";
  15. //是否允许文件管理器删除目录;
  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. if (($newname != $oldname) && is_writable($oldname)) {
  31. rename($oldname, $newname);
  32. }
  33. ShowMsg("成功更改一个文件名!", "file_manage_main.php?activepath=" . $this->activeDir);
  34. return 0;
  35. }
  36. //创建新目录
  37. function NewDir($dirname)
  38. {
  39. $newdir = $dirname;
  40. $dirname = $this->baseDir . $this->activeDir . "/" . $dirname;
  41. if (is_writable($this->baseDir . $this->activeDir)) {
  42. MkdirAll($dirname, $GLOBALS['cfg_dir_purview']);
  43. CloseFtp();
  44. ShowMsg("成功创建一个新目录!", "file_manage_main.php?activepath=" . $this->activeDir . "/" . $newdir);
  45. return 1;
  46. } else {
  47. ShowMsg("创建新目录失败,因为这个位置不允许写入!", "file_manage_main.php?activepath=" . $this->activeDir);
  48. return 0;
  49. }
  50. }
  51. /**
  52. * 移动文件
  53. *
  54. * @access public
  55. * @param string $mfile 文件
  56. * @param string $mpath 路径
  57. * @return string
  58. */
  59. function MoveFile($mfile, $mpath)
  60. {
  61. if ($mpath != "" && !preg_match("#\.\.#", $mpath)) {
  62. $oldfile = $this->baseDir . $this->activeDir . "/$mfile";
  63. $mpath = str_replace("\\", "/", $mpath);
  64. $mpath = preg_replace("#\/{1,}#", "/", $mpath);
  65. if (!preg_match("#^/#", $mpath)) {
  66. $mpath = $this->activeDir . "/" . $mpath;
  67. }
  68. $truepath = $this->baseDir . $mpath;
  69. if (is_readable($oldfile) && is_readable($truepath) && is_writable($truepath)) {
  70. if (is_dir($truepath)) {
  71. copy($oldfile, $truepath . "/$mfile");
  72. } else {
  73. MkdirAll($truepath, $GLOBALS['cfg_dir_purview']);
  74. CloseFtp();
  75. copy($oldfile, $truepath . "/$mfile");
  76. }
  77. unlink($oldfile);
  78. ShowMsg("成功移动文件!", "file_manage_main.php?activepath=$mpath", 0, 1000);
  79. return 1;
  80. } else {
  81. ShowMsg("移动文件 $oldfile -&gt; $truepath/$mfile 失败,可能是某个位置权限不足!", "file_manage_main.php?activepath=$mpath", 0, 1000);
  82. return 0;
  83. }
  84. } else {
  85. ShowMsg("对不起,你移动的路径不合法!", "-1", 0, 5000);
  86. return 0;
  87. }
  88. }
  89. /**
  90. * 删除目录
  91. *
  92. * @param unknown_type $indir
  93. */
  94. function RmDirFiles($indir)
  95. {
  96. if (!is_dir($indir)) {
  97. return;
  98. }
  99. $dh = dir($indir);
  100. while ($filename = $dh->read()) {
  101. if ($filename == "." || $filename == "..") {
  102. continue;
  103. } else if (is_file("$indir/$filename")) {
  104. @unlink("$indir/$filename");
  105. } else {
  106. $this->RmDirFiles("$indir/$filename");
  107. }
  108. }
  109. $dh->close();
  110. @rmdir($indir);
  111. }
  112. /**
  113. * 获得某目录合符规则的文件
  114. *
  115. * @param unknown_type $indir
  116. * @param unknown_type $fileexp
  117. * @param unknown_type $filearr
  118. */
  119. function GetMatchFiles($indir, $fileexp, &$filearr)
  120. {
  121. $dh = dir($indir);
  122. while ($filename = $dh->read()) {
  123. $truefile = $indir . '/' . $filename;
  124. if ($filename == "." || $filename == "..") {
  125. continue;
  126. } else if (is_dir($truefile)) {
  127. $this->GetMatchFiles($truefile, $fileexp, $filearr);
  128. } else if (substr($filename, -strlen($fileexp)) === $fileexp) {
  129. $filearr[] = $truefile;
  130. }
  131. }
  132. $dh->close();
  133. }
  134. /**
  135. * 删除文件
  136. *
  137. * @param unknown_type $filename
  138. * @return unknown
  139. */
  140. function DeleteFile($filename)
  141. {
  142. $filename = $this->baseDir . $this->activeDir . "/$filename";
  143. if (is_file($filename)) {
  144. @unlink($filename);
  145. $t = "文件";
  146. } else {
  147. $t = "目录";
  148. if ($this->allowDeleteDir == 1) {
  149. $this->RmDirFiles($filename);
  150. } else {
  151. // 完善用户体验,by:sumic
  152. ShowMsg("系统禁止删除" . $t . "!", "file_manage_main.php?activepath=" . $this->activeDir);
  153. exit;
  154. }
  155. }
  156. ShowMsg("成功删除一个" . $t . "!", "file_manage_main.php?activepath=" . $this->activeDir);
  157. return 0;
  158. }
  159. }
  160. //目录文件大小检测类
  161. class SpaceUse
  162. {
  163. var $totalsize = 0;
  164. function checksize($indir)
  165. {
  166. $dh = dir($indir);
  167. while ($filename = $dh->read()) {
  168. if (!preg_match("#^\.#", $filename)) {
  169. if (is_dir("$indir/$filename")) {
  170. $this->checksize("$indir/$filename");
  171. } else {
  172. $this->totalsize = $this->totalsize + filesize("$indir/$filename");
  173. }
  174. }
  175. }
  176. }
  177. function setkb($size)
  178. {
  179. $size = $size / 1024;
  180. if ($size > 0) {
  181. list($t1, $t2) = explode(".", $size);
  182. $size = $t1 . "." . substr($t2, 0, 1);
  183. }
  184. return $size;
  185. }
  186. function setmb($size)
  187. {
  188. $size = $size / 1024 / 1024;
  189. if ($size > 0) {
  190. list($t1, $t2) = explode(".", $size);
  191. $size = $t1 . "." . substr($t2, 0, 2);
  192. }
  193. return $size;
  194. }
  195. }