better-staridc-MNBT
1<?php
2/************************************************
3 * Amysql FTP - AMFTP 3.0
4 * amh.sh
5 * Update:2021-01-15
6 *
7 */
8
9class amftp
10{
11 public $amftp_ftp_connect = null;
12 public $amftp_am_ftp_pwd = null;
13
14 // 建立ftp连接
15 function am_ftp_connect($ftp_ip, $ftp_port)
16 {
17 $this -> amftp_ftp_connect = @ftp_connect($ftp_ip, $ftp_port);
18 ftp_set_option($this -> amftp_ftp_connect, FTP_TIMEOUT_SEC, 20); // 超时
19 Return $this -> amftp_ftp_connect;
20 }
21
22
23 // 登录
24 function am_ftp_login($ftp_user, $ftp_pass, $ftp_pasv = false)
25 {
26 if(@ftp_login($this -> amftp_ftp_connect, $ftp_user, $ftp_pass))
27 {
28 ftp_pasv($this -> amftp_ftp_connect, $ftp_pasv);
29 $this -> amftp_am_ftp_pwd = $this -> am_ftp_pwd();
30 Return true;
31 }
32 Return false;
33 }
34
35
36 // 文件相关 *************************************************************
37
38 // 更改权限
39 function am_ftp_chmod($mode, $file)
40 {
41 Return $this -> am_ftp_site("CHMOD {$mode} {$file}");
42 }
43 // 更改权限(目录递归)
44 function am_ftp_chmods($mode, $dir, $recursion = false)
45 {
46 if ($recursion)
47 {
48 $am_ftp_rawlist = $this -> am_ftp_rawlist($dir);
49 foreach ($am_ftp_rawlist as $key=>$val)
50 {
51 $row = functions::Grawlistline($val);
52 if(!in_array($row['dirfilename'], array('.', '..')))
53 {
54 $this -> am_ftp_chmod($mode, $dir . '/' . $row['dirfilename']);
55
56 if ($row['dirorfile'] == 'd')
57 $this -> am_ftp_chmods($mode, $dir . '/' . $row['dirfilename'], $recursion);
58 }
59 }
60 }
61 Return $this -> am_ftp_chmod($mode, $dir);
62 }
63
64 // 移动
65 function am_ftp_move($from, $to)
66 {
67 $this -> am_ftp_raw("RNFR {$from}");
68 $status = $this -> am_ftp_raw("RNTO {$to}");
69 return strpos(implode(',', $status), 'successfully renamed') !== false ? true : false;
70 }
71
72 // 删除文件
73 function am_ftp_delete($file)
74 {
75 Return @ftp_delete($this -> amftp_ftp_connect, $file);
76 }
77
78 // 取得文件大小
79 function am_ftp_size($file)
80 {
81 Return @ftp_size($this -> amftp_ftp_connect, $file);
82 }
83
84 // 重命名
85 function am_ftp_rename($from, $to)
86 {
87 Return @ftp_rename($this -> amftp_ftp_connect, $from, $to);
88 }
89
90 // 目录相关 *************************************************************
91
92 // 返回父目录
93 function am_ftp_cdup()
94 {
95 Return @ftp_cdup($this -> amftp_ftp_connect);
96 }
97
98 // 改变目录
99 function am_ftp_chdir($directory)
100 {
101 Return @ftp_chdir($this -> amftp_ftp_connect, $directory);
102 }
103
104 // 当前路径
105 function am_ftp_pwd()
106 {
107 Return @ftp_pwd($this -> amftp_ftp_connect);
108 }
109
110 // 建立文件夹
111 function am_ftp_mkdir($dirname)
112 {
113 Return @ftp_mkdir($this -> amftp_ftp_connect, $dirname);
114 }
115
116 // 删除目录
117 function am_ftp_rmdir($directory)
118 {
119 $list = $this -> am_ftp_nlist($directory);
120 foreach ($list as $k=>$v)
121 {
122 $filename = basename($v);
123 if ($filename != '.' && $filename != '..')
124 {
125 if(!$this -> am_ftp_delete($v))
126 $this -> am_ftp_rmdir($v);
127 }
128 }
129 Return @ftp_rmdir($this -> amftp_ftp_connect, $directory);
130 }
131
132 // 文件列表
133 function am_ftp_nlist($directory)
134 {
135 Return @ftp_nlist($this -> amftp_ftp_connect, $directory);
136 }
137
138 // 文件详细列表
139 function am_ftp_rawlist($directory)
140 {
141 Return @ftp_rawlist($this -> amftp_ftp_connect, $directory);
142 }
143
144
145 // 命令相关 *************************************************************
146
147 // 执行命令
148 function am_ftp_exec($command)
149 {
150 Return ftp_exec($this -> amftp_ftp_connect, $command);
151 }
152
153 // 执行raw命令
154 function am_ftp_raw($command)
155 {
156 Return ftp_raw($this -> amftp_ftp_connect, $command);
157 }
158
159 // 执行site命令
160 function am_ftp_site($command)
161 {
162 Return ftp_site($this -> amftp_ftp_connect, $command);
163 }
164
165 // 上传&下载 *************************************************************
166 // 上传文件(包含目录)
167 function am_ftp_puts($remote, $local)
168 {
169 $list = array_diff(scandir($local), array('.', '..'));
170 foreach ($list as $val)
171 {
172 $remote_file_dir = $remote . $val;
173 $file_or_dir = $local . $val;
174
175 if (is_dir($file_or_dir))
176 {
177 $this -> am_ftp_mkdir($remote_file_dir);
178 if(!$this -> am_ftp_puts($remote_file_dir . '/', $file_or_dir . '/'))
179 Return false;
180 }
181 else
182 {
183 if(!$this -> am_ftp_put($remote_file_dir, $file_or_dir))
184 Return false;
185 }
186 }
187 unset($list);
188 Return true;
189 }
190
191
192 // 上传文件
193 function am_ftp_put($remote, $local)
194 {
195 Return @ftp_put($this -> amftp_ftp_connect, $remote, $local, FTP_BINARY);
196 }
197
198
199 // 下载文件(包含目录) **********************
200 function am_ftp_gets($local, $remote)
201 {
202 // /tmp/tmp_tar/ /modules
203 $path = $local . $remote . '/';
204 functions::mkdirs($path);
205
206 $am_ftp_rawlist = $this -> am_ftp_rawlist($remote);
207 foreach ($am_ftp_rawlist as $key=>$val)
208 {
209 $row = functions::Grawlistline($val);
210 if(!in_array($row['dirfilename'], array('.', '..')))
211 {
212 if ($row['dirorfile'] == 'd')
213 $this -> am_ftp_gets($local, $remote . '/' . $row['dirfilename']);
214 else
215 $this -> am_ftp_get($path, $remote . '/' . $row['dirfilename']);
216 }
217 }
218 }
219
220 // 下载文件
221 function am_ftp_get($local, $remote)
222 {
223 $filename = explode('/', $remote);
224 $filename = $filename[count($filename) -1];
225 functions::mkdirs($local);
226
227 if(ftp_get($this -> amftp_ftp_connect, $local . $filename , $remote, FTP_BINARY))
228 Return $local . $filename;
229 else
230 Return false;
231 }
232
233 // *************************
234
235
236 // 释放
237 function __destruct()
238 {
239 ($this -> amftp_ftp_connect && ftp_close($this -> amftp_ftp_connect));
240 }
241}
242
243?>