-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftp.php
More file actions
426 lines (397 loc) · 13.5 KB
/
ftp.php
File metadata and controls
426 lines (397 loc) · 13.5 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
/* (C)2005-2021 FoundPHP Framework.
* name: Ease Template
* weburl: http://www.FoundPHP.com
* mail: master@FoundPHP.com
* author: 孟大川
* version: 1.21.0222
* start: 2017-02-24
* update: 2021-02-22
* payment: Free 免费
* This is not a freeware, use is subject to license terms.
* 此软件为授权使用软件,请参考软件协议。
* http://www.foundphp.com/?m=agreement
示例:
$set = array(
'host' => '127.0.0.1', //服务器
'port' => '21', //端口
'ssh' => 1, //ssh 1启动,0关闭
'username' => 'test', //帐号
'password' => '123123', //密码
'passive' => 1, //被动模式:1启动,0关闭
'timeout' => '3', //超时时长
'language' => 'cn', //语言包
'logs' => 'ftp_log.txt', //日志
);
$ftp = new FoundPHP_ftp($set);
//获取目录
$dirs = $ftp->get_dir('/');
print_r($dirs);
//上传文件
$ftp->put('a1.jpg','113.jpg');
//下载文件
$ftp->get('113.jpg','a12.jpg');
//删除文件
$ftp->del('113.jpg');
//建立文件夹
$ftp->mk_dir('test/1111');
//删除目录
$ftp->rm_dir('test/1111');
//改名
//$ftp->name('112.jpg','a.jpg');
//关闭链接
$ftp->close();
*/
class FoundPHP_ftp{
public $ssh = 0; //普通ftp:0,ssh ftp:1
public $links = ''; //链接库
public $host = ''; //ftp地址
public $user = ''; //ftp帐号
public $logs = 'logs.txt'; //日志文件
public $language = 'cn'; //语言
public $lang = array();
//构造快递方法
function __construct($ary=array()) {
//默认端口
if ((int)$ary['port']==0) {
$ary['port'] = 21;
}
//默认超时
if ((int)$ary['timeout']==0) {
$ary['timeout'] = 5;
}
//服务器
$this->host = $ary['host'];
$this->user = $ary['username'];
//载入语言包
if ($ary['language']!=''){
$this->language = $ary['language'];
}
//日志位置
if ($ary['logs']!=''){
$this->logs = $ary['logs'];
}
//服务器的连接模式
switch((int)$ary['ssh']){
//ssh加密模式
case 1:
$methods = array(
'hostkey'=>'ssh-rsa,ssh-dss',
'client_to_server' => array(
'crypt' => 'aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-cbc,blowfish-cbc',
'comp' => 'none'
),
'server_to_client' => array(
'crypt' => 'aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-cbc,blowfish-cbc',
'comp' => 'none'
)
);
//连接服务器
$this->connect = ssh2_connect($ary['host'],$ary['port'],$methods);
if (!$this->connect){
$error = $this->lang('host_not_existent1').$ary['host'].$this->lang('host_not_existent2');
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
$this->logs($ary['host'].$this->lang('host_connect'));
//登录
if (@ssh2_auth_password($this->connect, $ary['username'], $ary['password'])) {
$this->logs($ary['username'].$this->lang('login_connect'));
$this->links = ssh2_sftp($this->connect);
} else {
$error = $ary['username'].$this->lang('login_error');
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
//连接模式
$this->ssh = 1;
break;
//标准FTP
default:
//连接服务器
$this->links = ftp_connect ($ary['host'],$ary['port'],$ary['timeout']);
if (!$this->links){
$error = $this->lang('host_not_existent1').$ary['host'].$this->lang('host_not_existent2');
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
$this->logs($ary['host'].$this->lang('host_connect'));
//被动模式
if ($ary['passive']==1){
ftp_pasv($this->links, true);
$this->logs($this->lang('passive'));
}
//登录
if (@ftp_login($this->links, $ary['username'], $ary['password'])) {
$this->logs($ary['username'].$this->lang('login_connect'));
} else {
$error = $ary['username'].$this->lang('login_error');
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
//连接模式
$this->ssh = 0;
}
}
function lang($id=''){
$ftplang = dirname(__FILE__).'/language/ftp_'.$this->language.'.php';
if (is_file($ftplang)){
include_once($ftplang);
$lang = $GLOBALS['FoundPHP_FTP_Lang'];
}else{
$lang = array(
'sorry' => '抱歉,',
'host_connect' => ' 服务器连接成功。',
'host_not_existent1' => '无法连接 ',
'host_not_existent2' => ',请检查配置参数。',
'login_connect' => ' 登录成功。',
'login_error' => ' 登录失败或密码错误。',
'close' => ' 服务器连接关闭。',
'not_file' => '没有找到上传文件:',
'put_end' => '上传成功:',
'put_error' => '没有权限或上传失败:',
'get_end' => '下载成功:',
'get_error' => '没有权限、文件错误或下载失败:',
'del_end' => '删除文件成功: ',
'del_error' => '删除文件失败或不存在: ',
'mkdir_end' => '目录建立成功:',
'mkdir_error' => '目录建立失败:',
'rmdir_end' => '目录删除成功:',
'rmdir_error' => '目录删除失败或不存在:',
'rmname_set' => '未设置旧名称与新名称',
'rmname_end' => '改名成功:',
'rmname_error' => '改名失败,目录或文件不存在:',
'passive' => '开启被动模式',
);
}
return $lang[$id];
}
/*获得指定目录
dirs 目录地址,根目录/
*/
function get_dir($dirs='/'){
if ($this->ssh==1){
//获取根目录
if ($dirs=='/'){
$dirs = './';
}
$dirHandle = opendir("ssh2.sftp://$this->links/$dirs");
while (false !== ($file = readdir($dirHandle))) {
$statinfo = ssh2_sftp_stat($this->links, $dirs.'/'.$file);
if ($file != '.' && $file != '..' && !empty($file)) {
if (!stristr($file,'.')){
$info['name'] = iconv('gbk','UTF-8',$file);
$result['dir'][] = $info;
}else{
$info['name'] = iconv('gbk','UTF-8',$file);
//$info['size'] = filesize("ssh2.sftp://$this->links/$dirs/$file");
$result['file'][] = $info;
}
}
}
}else{
$get_list = ftp_rawlist($this->links, $dirs);
if ($get_list){
foreach($get_list AS $k=>$v) {
$fileinfo = preg_split("/[\s]+/", $v, 9);
if ($fileinfo[8] != '.' && $fileinfo[8] != '..' && !empty($fileinfo[8])) {
//文件夹
if (!stristr($fileinfo[8],'.')){
$info['name'] = iconv('gbk','UTF-8',$fileinfo[8]);
$info['chmod'] = $fileinfo[0];
$info['owner'] = $fileinfo[2];
$info['group'] = $fileinfo[3];
$info['day'] = $fileinfo[5].' '.$fileinfo[6];
$info['time'] = $fileinfo[7];
$result['dir'][] = $info;
}else{
//文件
$info['name'] = iconv('gbk','UTF-8',$fileinfo[8]);
$info['chmod'] = $fileinfo[0];
$info['owner'] = $fileinfo[2];
$info['group'] = $fileinfo[3];
$info['size'] = $fileinfo[4];
$info['day'] = $fileinfo[5].' '.$fileinfo[6];
$info['time'] = $fileinfo[7];
$result['file'][] = $info;
}
}
}
}
}
return $result;
}
/*上传文件
file_name 本地文件(目录配文件)
save_name 远程文件(目录配文件)
*/
function put($file_name='',$save_name=''){
if (!is_file($file_name)){
$error = $this->lang('sorry').$this->lang('not_file').$file_name;
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
if ($this->ssh==1){
if (copy($save_name,"ssh2.sftp://{$this->links}".$file_name)) {
$this->logs($this->lang('get_end').$file_name.' => '.$save_name);
return array('code'=>1,'file_name'=>$save_name);
} else {
$error = $this->lang('sorry').$this->lang('put_error').$file_name.' => '.$save_name;
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
}else{
if (ftp_put($this->links, $save_name, $file_name, FTP_BINARY)) {
$this->logs($this->lang('put_end').$file_name.' => '.$save_name);
return array('code'=>1,'file_name'=>$save_name);
} else {
$error = $this->lang('sorry').$this->lang('put_error').$file_name.' => '.$save_name;
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
}
}
/*下载文件
file_name 远程文件(目录配文件)
save_name 本地文件(目录配文件)
*/
function get($file_name='',$save_name=''){
if ($save_name==''){
$save_name = $file_name;
}
if ($this->ssh==1){
if (copy("ssh2.sftp://$this->links/$file_name", $save_name)) {
$this->logs($this->lang('get_end').$file_name.' => '.$save_name);
return array('code'=>1,'file_name'=>$save_name);
} else {
$error = $this->lang('sorry').$this->lang('get_error').$file_name.' => '.$save_name;
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
}else{
if (ftp_get($this->links, $save_name, $file_name, FTP_BINARY)) {
$this->logs($this->lang('get_end').$file_name.' => '.$save_name);
return array('code'=>1,'file_name'=>$save_name);
} else {
$error = $this->lang('sorry').$this->lang('get_error').$file_name.' => '.$save_name;
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
}
}
/*删除文件
file_name 远程文件(目录配文件)
*/
function del($file_name=''){
if ($this->ssh==1){
if (ssh2_sftp_unlink($this->links, $file_name)) {
$this->logs($this->lang('del_end').$file_name);
return array('code'=>1,'file_name'=>$file_name);
} else {
$error = $this->lang('sorry').$this->lang('del_error').$file_name;
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
}else{
if (ftp_delete($this->links, $file_name)) {
$this->logs($this->lang('del_end').$file_name);
return array('code'=>1,'file_name'=>$file_name);
} else {
$error = $this->lang('sorry').$this->lang('del_error').$file_name;
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
}
}
/*建立文件夹
name 文件夹名称
*/
function mk_dir($name=''){
if ($this->ssh==1){
if (ssh2_sftp_mkdir($this->links, $name,0777)) {
$this->logs($this->lang('mkdir_end').$name);
return array('code'=>1,'dir_name'=>$name);
} else {
$error = $this->lang('sorry').$this->lang('mkdir_error').$name;
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
}else{
if (ftp_mkdir($this->links, $name)) {
$this->logs($this->lang('mkdir_end').$name);
return array('code'=>1,'dir_name'=>$name);
} else {
$error = $this->lang('sorry').$this->lang('mkdir_error').$name;
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
}
}
/*删除文件夹
name 文件夹名称
*/
function rm_dir($name=''){
if ($this->ssh==1){
if (@ssh2_sftp_rmdir($this->links, $name)) {
$this->logs($this->lang('rmdir_end').$name);
return array('code'=>1,'dir_name'=>$name);
} else {
$error = $this->lang('sorry').$this->lang('rmdir_error').$name;
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
}else{
if (@ftp_rmdir($this->links, $name)) {
$this->logs($this->lang('rmdir_end').$name);
return array('code'=>1,'dir_name'=>$name);
} else {
$error = $this->lang('sorry').$this->lang('rmdir_error').$name;
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
}
}
/*目录或文件改名
name 文件夹、文件名称
*/
function name($old_name='',$new_name=''){
if (trim($old_name)=='' || trim($new_name)==''){
$error = $this->lang('sorry').$this->lang('rmname_set');
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
if ($this->ssh==1){
if (@ssh2_sftp_rename($this->links, $old_name,$new_name)) {
$this->logs($this->lang('rmname_end').$old_name.' => '.$new_name);
return array('code'=>1,'new_name'=>$new_name);
} else {
$error = $this->lang('sorry').$this->lang('rmname_error').$old_name.' => '.$new_name;
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
}else{
if (@ftp_rename($this->links, $old_name,$new_name)) {
$this->logs($this->lang('rmname_end').$old_name.' => '.$new_name);
return array('code'=>1,'new_name'=>$new_name);
} else {
$error = $this->lang('sorry').$this->lang('rmname_error').$old_name.' => '.$new_name;
$this->logs($error);
function_exists('found_shutdown')?found_shutdown($error):die($error);
}
}
}
//关闭FTP
function close(){
$this->logs($this->host.$this->lang('close')."\r\n----------------------------------------\r\n");
if ($this->ssh==0){
ftp_close($this->links);
}
}
//日志
function logs($msg=''){
if ($msg){
$data = dates(time())."\t".$msg."\r\n";
$GLOBALS['tpl']->writer($this->logs,$data,'a+');
}
}
}