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

243 lines
5.6KB

  1. <?php if(!defined('DEDEINC')) exit('dedecms');
  2. /**
  3. * 文件处理小助手
  4. *
  5. * @version $Id: file.helper.php 1 2010-07-05 11:43:09Z tianya $
  6. * @package DedeCMS.Helpers
  7. * @copyright Copyright (c) 2007 - 2020, DesDev, Inc.
  8. * @license http://help.dedecms.com/usersguide/license.html
  9. * @link http://www.dedecms.com
  10. */
  11. $g_ftpLink = false;
  12. /**
  13. * 使用FTP方法创建文件夹目录
  14. *
  15. * @param string $truepath 真实目标地址
  16. * @param string $mmode 创建模式
  17. * @param string $isMkdir 是否创建目录
  18. * @return bool
  19. */
  20. if ( ! function_exists('FtpMkdir'))
  21. {
  22. function FtpMkdir($truepath,$mmode,$isMkdir=true)
  23. {
  24. global $cfg_basedir,$cfg_ftp_root,$g_ftpLink;
  25. OpenFtp();
  26. $ftproot = preg_replace('/'.$cfg_ftp_root.'$/', '', $cfg_basedir);
  27. $mdir = preg_replace('/^'.$ftproot.'/', '', $truepath);
  28. if($isMkdir)
  29. {
  30. ftp_mkdir($g_ftpLink, $mdir);
  31. }
  32. return ftp_site($g_ftpLink, "chmod $mmode $mdir");
  33. }
  34. }
  35. /**
  36. * 改变目录模式
  37. *
  38. * @param string $truepath 真实地址
  39. * @param string $mmode 模式
  40. * @return bool
  41. */
  42. if ( ! function_exists('FtpChmod'))
  43. {
  44. function FtpChmod($truepath, $mmode)
  45. {
  46. return FtpMkdir($truepath, $mmode, false);
  47. }
  48. }
  49. /**
  50. * 打开FTP链接,打开之前确保已经设置好了FTP相关的配置信息
  51. *
  52. * @return void
  53. */
  54. if ( ! function_exists('OpenFtp'))
  55. {
  56. function OpenFtp()
  57. {
  58. global $cfg_basedir,$cfg_ftp_host,$cfg_ftp_port, $cfg_ftp_user,$cfg_ftp_pwd,$cfg_ftp_root,$g_ftpLink;
  59. if(!$g_ftpLink)
  60. {
  61. if($cfg_ftp_host=='')
  62. {
  63. echo "由于你的站点的PHP配置存在限制,程序尝试用FTP进行目录操作,你必须在后台指定FTP相关的变量!";
  64. exit();
  65. }
  66. $g_ftpLink = ftp_connect($cfg_ftp_host,$cfg_ftp_port);
  67. if(!$g_ftpLink)
  68. {
  69. echo "连接FTP失败!";
  70. exit();
  71. }
  72. if(!ftp_login($g_ftpLink,$cfg_ftp_user,$cfg_ftp_pwd))
  73. {
  74. echo "登录FTP失败!";
  75. exit();
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * 关闭FTP链接
  82. *
  83. * @return void
  84. */
  85. if ( ! function_exists('CloseFtp'))
  86. {
  87. function CloseFtp()
  88. {
  89. global $g_ftpLink;
  90. if($g_ftpLink)
  91. {
  92. @ftp_quit($g_ftpLink);
  93. }
  94. }
  95. }
  96. /**
  97. * 创建所有目录
  98. *
  99. * @param string $truepath 真实地址
  100. * @param string $mmode 模式
  101. * @return bool
  102. */
  103. if ( ! function_exists('MkdirAll'))
  104. {
  105. function MkdirAll($truepath,$mmode)
  106. {
  107. global $cfg_ftp_mkdir,$isSafeMode,$cfg_dir_purview;
  108. if( $isSafeMode || $cfg_ftp_mkdir=='Y' )
  109. {
  110. return FtpMkdir($truepath, $mmode);
  111. }
  112. else
  113. {
  114. if(!file_exists($truepath))
  115. {
  116. mkdir($truepath, $cfg_dir_purview);
  117. chmod($truepath, $cfg_dir_purview);
  118. return true;
  119. }
  120. else
  121. {
  122. return true;
  123. }
  124. }
  125. }
  126. }
  127. /**
  128. * 更改所有模式
  129. *
  130. * @access public
  131. * @param string $truepath 文件路径
  132. * @param string $mmode 模式
  133. * @return string
  134. */
  135. if ( ! function_exists('ChmodAll'))
  136. {
  137. function ChmodAll($truepath,$mmode)
  138. {
  139. global $cfg_ftp_mkdir,$isSafeMode;
  140. if( $isSafeMode || $cfg_ftp_mkdir=='Y' )
  141. {
  142. return FtpChmod($truepath, $mmode);
  143. }
  144. else
  145. {
  146. return chmod($truepath, '0'.$mmode);
  147. }
  148. }
  149. }
  150. /**
  151. * 创建目录
  152. *
  153. * @param string $spath 创建的文件夹
  154. * @return bool
  155. */
  156. if ( ! function_exists('CreateDir'))
  157. {
  158. function CreateDir($spath)
  159. {
  160. if(!function_exists('SpCreateDir'))
  161. {
  162. require_once(DEDEINC.'/inc/inc_fun_funAdmin.php');
  163. }
  164. return SpCreateDir($spath);
  165. }
  166. }
  167. /**
  168. * 写文件
  169. *
  170. * @access public
  171. * @param string $file 文件名
  172. * @param string $content 内容
  173. * @param int $flag 标识
  174. * @return string
  175. */
  176. if ( ! function_exists('PutFile'))
  177. {
  178. function PutFile($file, $content, $flag = 0)
  179. {
  180. $pathinfo = pathinfo ( $file );
  181. if (! empty ( $pathinfo ['dirname'] ))
  182. {
  183. if (file_exists ( $pathinfo ['dirname'] ) === FALSE)
  184. {
  185. if (@mkdir ( $pathinfo ['dirname'], 0777, TRUE ) === FALSE)
  186. {
  187. return FALSE;
  188. }
  189. }
  190. }
  191. if ($flag === FILE_APPEND)
  192. {
  193. return @file_put_contents ( $file, $content, FILE_APPEND );
  194. }
  195. else
  196. {
  197. return @file_put_contents ( $file, $content, LOCK_EX );
  198. }
  199. }
  200. }
  201. /**
  202. * 用递归方式删除目录
  203. *
  204. * @access public
  205. * @param string $file 目录文件
  206. * @return string
  207. */
  208. if ( ! function_exists('RmRecurse'))
  209. {
  210. function RmRecurse($file)
  211. {
  212. if (is_dir($file) && !is_link($file))
  213. {
  214. foreach(glob($file . '/*') as $sf)
  215. {
  216. if (!RmRecurse($sf))
  217. {
  218. return false;
  219. }
  220. }
  221. return @rmdir($file);
  222. } else {
  223. return @unlink($file);
  224. }
  225. }
  226. }