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

207 lines
5.3KB

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