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

208 lines
5.3KB

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