Ftp.class.php
4.99 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
dirname(__FILE__).'/config/config.php';
class Util_Ftp
{
private $host = "localhost";//FTP HOST
private $port = "21"; //FTP port
private $user = "Anonymous";//FTP user
private $password = "Email"; //FTP password
private $link_id = ""; //FTP hand
private $is_login = ""; //is login
private $debug = 1;
private $local_dir = ""; //local path for upload or download
private $rootdir = ""; //FTP root path of FTP server
private $dir = "/"; //FTP current path
/**
* 初始化函数
*
* @param string $user
* @param string $password
* @param string $host
* @param int $port
*/
public function __construct($user = FTP_SERVER_USER, $password = FTP_SERVER_PASSWORD,
$host = FTP_SERVER_HOST, $port = FTP_SERVER_PORT)
{
if(strlen($user))
{
$this->user = $user;
}
if(strlen($password))
{
$this->password = $password;
}
if(strlen($host))
{
$this->host = $host;
}
if(is_numeric($port))
{
$this->port = $port;
}
$this->login();
$this->rootdir = $this->pwd();
$this->dir = $this->rootdir;
}
/**
* 提示
*
* @param string $msg
* @param string $line
* @return unknown_type
*/
public function halt($msg,$line=__LINE__)
{
echo "FTP Error in line:$line<br/>\n";
echo "FTP Error message:$msg<br/>\n";
exit();
}
/**
* 登录
*/
public function login()
{
if(!$this->link_id)
{
$this->link_id = ftp_connect($this->host,$this->port) or $this->halt("can not connect to host:$this->host:$this->port",__LINE__);
}
if(!$this->is_login)
{
//记录时间
$startTime = time();
while(1)
{
$this->is_login = @ftp_login($this->link_id, $this->user, $this->pass);
//如果时间超过60秒,则可以有问题
if((time()-$startTime)>=60)
{
echo 'error';
exit();
}
if($this->is_login)
{
//echo "Connected as $ftp_user@$ftp_server\n";
break;
}
}
}
}
/**
* 返回远程 FTP 服务器的系统类型
*
* @return int
*/
public function systype()
{
return ftp_systype($this->link_id);
}
/**
* 返回当前目录名
*
* @return string
*/
public function pwd()
{
$this->login();
$dir = ftp_pwd($this->link_id);
$this->dir = $dir;
return $dir;
}
/**
* 切换到当前目录的父目录
*
* @return boolean
*/
public function cdup()
{
$this->login();
$isok = ftp_cdup($this->link_id);
if($isok) $this->dir = $this->pwd();
return $isok;
}
/**
* 切换当前目录
*
* @param string$dir
* @return boolean
*/
public function cd($dir)
{
$this->login();
$isok = ftp_chdir($this->link_id,$dir);
if($isok) $this->dir = $dir;
return $isok;
}
/**
* 返回给定目录的文件列表
*
* @param string $dir
* @return array
*/
public function nlist($dir="")
{
$this->login();
if(!$dir) $dir = ".";
$arr_dir = ftp_nlist($this->link_id,$dir);
return $arr_dir;
}
/**
* 返回指定目录下文件的详细列表
*
* @param string $dir
* @return array
*/
public function rawlist($dir="/")
{
$this->login();
$arr_dir = ftp_rawlist($this->link_id,$dir);
return $arr_dir;
}
/**
* 建立新目录
*
* @param string $dir
* @return boolean
*/
public function mkdir($dir)
{
$this->login();
return @ftp_mkdir($this->link_id,$dir);
}
/**
* 返回指定文件的大小
*
* @param string $file
* @return int
*/
public function file_size($file)
{
$this->login();
$size = ftp_size($this->link_id,$file);
return $size;
}
/**
* 修改文件属性
*
* @param string $file
* @param int $mode
* @return boolean
*/
public function chmod($file,$mode=0666)
{
$this->login();
return ftp_chmod($this->link_id,$file,$mode);
}
/**
* 删除文件
*
* @param string $remote_file
* @return boolean
*/
public function delete($remote_file)
{
$this->login();
return @ftp_delete($this->link_id,$remote_file);
}
/**
* 下载
*
* @param string $local_file
* @param string $remote_file
* @param string $mode
* @return mix
*/
public function get($local_file,$remote_file,$mode=FTP_BINARY)
{
$this->login();
return @ftp_get($this->link_id,$local_file,$remote_file,$mode);
}
/**
* 上传
*
* @param $local_file
* @param $remote_file
* @param $mode
* @return boolean
*/
public function put($local_file, $remote_file, $mode=FTP_BINARY)
{
$this->login();
return @ftp_put($this->link_id,$remote_file,$local_file,$mode);
}
/**
* 上传数据
*
* @param string $remote_file
* @param string $data
* @param $mode
* @return boolean
*/
public function put_string($remote_file,$data,$mode=FTP_BINARY)
{
$this->login();
$tmp = "/tmp";//ini_get("session.save_path");
$tmpfile = tempnam($tmp,"tmp_");
$fp = @fopen($tmpfile,"w+");
if($fp)
{
fwrite($fp,$data);
fclose($fp);
}
else
{
return 0;
}
$isok = $this->put($tmpfile, $remote_file,FTP_BINARY);
@unlink($tmpfile);
return $isok;
}
/**
* 关闭
*/
public function close()
{
@ftp_quit($this->link_id);
}
}