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