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

247 lines
6.9KB

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