forked from damijanc/simple-ftp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleFtp.class.php
More file actions
254 lines (207 loc) · 5.88 KB
/
SimpleFtp.class.php
File metadata and controls
254 lines (207 loc) · 5.88 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
<?php
/*
Copyright 2013 Damijan Cavar
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
class SimpleFtp {
private $conn;
private $host;
private $port = 21;
private $user;
private $password;
private $timeout = 5;
private $connected = FALSE;
private $transfer_mode = FTP_BINARY;
/**
* Constructor
*/
public function __construct ($options) {
if (is_array($options)) {
if (array_key_exists('server', $options))
$this->host = $options['server'];
if (array_key_exists('user', $options))
$this->user = $options['user'];
if (array_key_exists('port', $options))
$this->port = $options['port'];
if (array_key_exists('pass', $options))
$this->password = $options['pass'];
}
}
public function set_timeout ($t = 5) {
if (is_numeric($t) && $t >= 1)
$this->timeout = floor($t);
}
private function check_variables () {
if (empty($this->host)) {
throw new Exception('Host not set !!!');
}
if (empty($this->user)) {
throw new Exception('User not set !!!');
}
if (!is_numeric($this->port)) {
throw new Exception('Port not set !!!');
}
if (empty($this->password)) {
throw new Exception('Password not set !!!');
}
}
public function transfer_mode ($m = FTP_BINARY) {
$this->transfer_mode = $m;
}
public function connect () {
$this->check_variables();
if ($this->connected == FALSE) {
$this->conn = ftp_connect($this->host, $this->port, $this->timeout);
if ($this->conn) {
// Open a session to an external ftp site
$login_result = ftp_login($this->conn, $this->user, $this->password);
if ($login_result) {
$this->connected = TRUE;
return;
}
}
}
$this->connected = FALSE;
throw new Exception('Failed to connect');
}
function ftp_parse_response ($response, &$errstr) {
if (!is_array($response)) {
$errstr = 'Parameter \$response must be an array';
return false;
}
foreach ($response as $r) {
$code = substr(trim($r), 0, 3);
if (!is_numeric($code)) {
$errstr = "$code is not a valid FTP code";
}
if ($code > 400) {
$errstr = $r;
return false;
}
}
return true;
}
public function disconnect () {
ftp_quit($this->conn);
$this->connected = FALSE;
}
public function cd ($folder) {
if (!$this->connected)
throw new Exception('You are not connected');
if (ftp_pwd($this->conn) != $folder) {
if (ftp_chdir($this->conn, $folder) != FALSE) {
return TRUE;
}
}
return FALSE;
}
public function pwd () {
if (!$this->connected)
throw new Exception('You are not connected');
$s = ftp_pwd($this->conn);
echo $s ? "$s\n" : "Can't query folder. \n";
}
public function ls () {
if (!$this->connected)
throw new Exception('You are not connected');
if ($arr = ftp_nlist($this->conn, '.')) {
foreach ($arr as $file) {
echo $file . "\n";
}
}
}
public function mv ($old_file, $new_file) {
if (!$this->connected)
throw new Exception('You are not connected');
if (ftp_rename($this->conn, $old_file, $new_file)) {
return TRUE;
}
else {
return FALSE;
}
}
public function rm ($file) {
if (!$this->connected)
throw new Exception('You are not connected');
if (ftp_delete($this->conn, $file)) {
return TRUE;
}
else {
return FALSE;
}
}
public function rmdir ($folder) {
if (!$this->connected)
throw new Exception('You are not connected');
if (ftp_rmdir($this->conn, $folder)) {
return TRUE;
}
else {
return FALSE;
}
}
public function chmod ($mode,$file) {
if (!$this->connected)
throw new Exception('You are not connected');
if (ftp_chmod($this->conn, $mode, $file) !== false) {
return TRUE;
}
else {
return FALSE;
}
}
public function mkdir ($folder) {
if (!$this->connected)
throw new Exception('You are not connected');
ftp_mkdir($this->conn, $folder);
}
public function put ($file) {
if (!$this->connected)
throw new Exception('You are not connected');
//open file pointer
$fp = fopen($file, 'r');
if ($fp === FALSE)
throw new Exception('file does not exist');
//get filename from path
$path_parts = pathinfo($file);
$ret = ftp_nb_fput($this->conn, $path_parts['basename'] , $fp, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// We could print some progress bar since we are not blocking
// Continue downloading...
$ret = ftp_nb_continue($this->conn);
}
if ($ret != FTP_FINISHED) {
echo "There was an error downloading the file...";
exit(1);
}
// close filepointer
fclose($fp);
}
public function get ($file, $remote_location) {
if (!$this->connected)
throw new Exception('You are not connected');
//open file pointer
$fp = fopen($file, 'w');
$ret = ftp_nb_fget($this->conn, $fp, $file, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// We could print some progress bar since we are not blocking
// Continue downloading...
$ret = ftp_nb_continue($this->conn);
}
if ($ret != FTP_FINISHED) {
echo "There was an error downloading the file...";
exit(1);
}
// close filepointer
fclose($fp);
}
}
?>