/dede/file_class.php

https://github.com/musicpop/cms · PHP · 247 lines · 191 code · 14 blank · 42 comment · 32 complexity · ebd17d5bf7d6e1c96bf7cc8194b68a59 MD5 · raw file

  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 - 2010, 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 ʧ°Ü£¬¿ÉÄÜÊÇij¸öλÖÃȨÏÞ²»×㣡","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. * »ñµÃijĿ¼ºÏ·û¹æÔòµÄÎļþ
  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. }