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

203 lines
6.6KB

  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. //是否允许文件管理器删除目录,默认为不允许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. CloseFtp();
  52. ShowMsg("成功创建一个新目录", "file_manage_main.php?activepath=".$this->activeDir."/".$newdir);
  53. return 1;
  54. } else {
  55. ShowMsg("创建新目录失败,因为这个位置不允许写入", "file_manage_main.php?activepath=".$this->activeDir);
  56. return 0;
  57. }
  58. }
  59. /**
  60. * 移动文件
  61. *
  62. * @access public
  63. * @param string $mfile 文件
  64. * @param string $mpath 路径
  65. * @return string
  66. */
  67. function MoveFile($mfile, $mpath)
  68. {
  69. if ($mpath != "" && !preg_match("#\.\.#", $mpath)) {
  70. $oldfile = $this->baseDir.$this->activeDir."/$mfile";
  71. $mpath = str_replace("\\", "/", $mpath);
  72. $mpath = preg_replace("#\/{1,}#", "/", $mpath);
  73. if (!preg_match("#^/#", $mpath)) {
  74. $mpath = $this->activeDir."/".$mpath;
  75. }
  76. $truepath = $this->baseDir.$mpath;
  77. if (is_readable($oldfile) && is_readable($truepath) && is_writable($truepath)) {
  78. if (is_dir($truepath)) {
  79. copy($oldfile, $truepath."/$mfile");
  80. } else {
  81. MkdirAll($truepath, $GLOBALS['cfg_dir_purview']);
  82. CloseFtp();
  83. copy($oldfile, $truepath."/$mfile");
  84. }
  85. unlink($oldfile);
  86. ShowMsg("成功移动文件", "file_manage_main.php?activepath=$mpath", 0, 1000);
  87. return 1;
  88. } else {
  89. ShowMsg("移动文件<span class='text-primary'>$oldfile</span>&gt;<span class='text-primary'>$truepath/$mfile</span>失败,可能是某个位置权限不足", "file_manage_main.php?activepath=$mpath", 0, 1000);
  90. return 0;
  91. }
  92. } else {
  93. ShowMsg("您移动的路径不合法", "-1", 0, 5000);
  94. return 0;
  95. }
  96. }
  97. /**
  98. * 删除目录
  99. *
  100. * @param unknown_type $indir
  101. */
  102. function RmDirFiles($indir)
  103. {
  104. if (!is_dir($indir)) {
  105. return;
  106. }
  107. $dh = dir($indir);
  108. while ($filename = $dh->read()) {
  109. if ($filename == "." || $filename == "..") {
  110. continue;
  111. } else if (is_file("$indir/$filename")) {
  112. @unlink("$indir/$filename");
  113. } else {
  114. $this->RmDirFiles("$indir/$filename");
  115. }
  116. }
  117. $dh->close();
  118. @rmdir($indir);
  119. }
  120. /**
  121. * 获得某目录合符规则的文件
  122. *
  123. * @param unknown_type $indir
  124. * @param unknown_type $fileexp
  125. * @param unknown_type $filearr
  126. */
  127. function GetMatchFiles($indir, $fileexp, &$filearr)
  128. {
  129. $dh = dir($indir);
  130. while ($filename = $dh->read()) {
  131. $truefile = $indir.'/'.$filename;
  132. if ($filename == "." || $filename == "..") {
  133. continue;
  134. } else if (is_dir($truefile)) {
  135. $this->GetMatchFiles($truefile, $fileexp, $filearr);
  136. } else if (substr($filename, -strlen($fileexp)) === $fileexp) {
  137. $filearr[] = $truefile;
  138. }
  139. }
  140. $dh->close();
  141. }
  142. /**
  143. * 删除文件
  144. *
  145. * @param unknown_type $filename
  146. * @return unknown
  147. */
  148. function DeleteFile($filename)
  149. {
  150. $filename = $this->baseDir.$this->activeDir."/$filename";
  151. if (is_file($filename)) {
  152. @unlink($filename);
  153. $t = "文件";
  154. } else {
  155. $t = "目录";
  156. if ($this->allowDeleteDir == 1) {
  157. $this->RmDirFiles($filename);
  158. } else {
  159. ShowMsg("系统禁止删除".$t."", "file_manage_main.php?activepath=".$this->activeDir);
  160. exit;
  161. }
  162. }
  163. ShowMsg("成功删除一个".$t."", "file_manage_main.php?activepath=".$this->activeDir);
  164. return 0;
  165. }
  166. }
  167. //目录文件大小检测类
  168. class SpaceUse
  169. {
  170. var $totalsize = 0;
  171. function checksize($indir)
  172. {
  173. $dh = dir($indir);
  174. while ($filename = $dh->read()) {
  175. if (!preg_match("#^\.#", $filename)) {
  176. if (is_dir("$indir/$filename")) {
  177. $this->checksize("$indir/$filename");
  178. } else {
  179. $this->totalsize = $this->totalsize + filesize("$indir/$filename");
  180. }
  181. }
  182. }
  183. }
  184. function setkb($size)
  185. {
  186. $size = $size / 1024;
  187. if ($size > 0) {
  188. list($t1, $t2) = explode(".", $size);
  189. $size = $t1.".".substr($t2, 0, 1);
  190. }
  191. return $size;
  192. }
  193. function setmb($size)
  194. {
  195. $size = $size / 1024 / 1024;
  196. if ($size > 0) {
  197. list($t1, $t2) = explode(".", $size);
  198. $size = $t1.".".substr($t2, 0, 2);
  199. }
  200. return $size;
  201. }
  202. }
  203. ?>