国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

210 linhas
6.3KB

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