This repository was archived by the owner on Sep 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftp.class.php
More file actions
340 lines (299 loc) · 8.68 KB
/
ftp.class.php
File metadata and controls
340 lines (299 loc) · 8.68 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
<?php
/**
* FTP Class
*
* Allows connections to FTP servers
*
* @author Giles Smith
* @copyright Copyright (c) 2010
*/
class ftp
{
/**
* @param array $errors An array of any errors
*/
public $errors = array();
private $conn_id;
private $server;
private $username;
private $password;
private $port;
private $passive;
/**
* Default Constructor
*
* @return bool
* @access public
* @param string $server The server hostname to connect to
* @param string $username The username required to access the FTP server
* @param string $password The password required to access the FTP server
* @param int $port The port number to connect to the FTP server on
* @param bool $passive Whether or not to use a passive or active connection
*/
public function __construct($server, $username, $password, $port = 21, $passive = FALSE)
{
$this->server = $server;
$this->username = $username;
$this->password = $password;
$this->port = $port;
$this->passive = $passive;
return TRUE;
}
/**
* Upload a local file to the remote server
*
* @access public
* @param string $source_file The local file to upload
* @param string $destination_file The remote location and name of the file
* @param string $transfer_mode optional Defaults to Binary connections but can use FTP_ASCII
*/
public function upload($source_file, $destination_file, $transfer_mode = FTP_BINARY)
{
if(!$this->open_connection()){return FALSE;}
// check local file
if(!is_file($source_file))
{
$this->errors[] = "Unable to find local file to send";
return FALSE;
}
// attempt to send file
if(!@ftp_put($this->conn_id, $destination_file, $source_file, $transfer_mode))
{
$this->errors[] = "Unable to send file to remote server, does destination folder exist?";
$this->close_connection();
return FALSE;
}
$this->close_connection();
return TRUE;
}
/**
* Download a a file from remote server to local file
*
* @access public
* @param string $source_file The remote file
* @param string $destination_file The local file to create
* @param string $transfer_mode optional Defaults to Binary connections but can use FTP_ASCII
*/
public function download($source_file, $destination_file, $transfer_mode = FTP_BINARY)
{
if(!$this->open_connection()){return FALSE;}
// download file
if(!@ftp_get($this->conn_id, $destination_file, $source_file, $transfer_mode))
{
$this->errors[] = "Unable to download file, does local folder exist";
$this->close_connection();
return FALSE;
}
$this->close_connection();
return TRUE;
}
/**
* Deletes a remote file
*
* @access public
* @param string $file The remote file to delete
*/
public function delete_file($file = '')
{
if(!$this->open_connection()){return FALSE;}
if(!@ftp_delete($this->conn_id, $file))
{
$this->errors[] = "Unable to delete remote file, have you checked permissions.";
$this->close_connection();
return FALSE;
}
$this->close_connection();
return TRUE;
}
/**
* Rename or move a file or a directory
*
*
* @return bool
* @access public
* @param string $source_file The file or folder to be renamed/moved
* @param string $renamed_file The destination or new name of the file/folder
*/
public function rename_or_move($source_file, $renamed_file)
{
if(!$this->open_connection()){return FALSE;}
if(!@ftp_rename($this->conn_id, $source_file, $renamed_file))
{
$this->errors[] = "Unable to rename/move file";
$this->close_connection();
return FALSE;
}
$this->close_connection();
return TRUE;
}
/**
* Create a remote directory
*
* @return bool
* @access public
* @param string $dir The path of the remote directory to create
*/
public function create_dir($dir)
{
if(!$this->open_connection()){return FALSE;}
if(ftp_mkdir($this->conn_id, $dir) === FALSE)
{
$this->errors[] = "Unable to create remote directory";
$this->close_connection();
return FALSE;
}
$this->close_connection();
return TRUE;
}
/**
* Delete a remote directory
*
* @return bool
* @access public
* @param string $dir The path of the remote directory to delete
*/
function delete_dir()
{
if(!$this->open_connection()){return FALSE;}
if(!@ftp_rmdir($this->conn_id, $dir))
{
$this->errors[] = "Unable to delete remote directory";
$this->close_connection();
return FALSE;
}
$this->close_connection();
return TRUE;
}
/**
* Set permissions on a file or directory
*
* @return bool
* @access public
* @param string $file The file or directory to modify
* @param int $chmod optional The permissions to apply Default 0644
*/
function set_permissions($file, $chmod = 0644)
{
if(!$this->open_connection()){return FALSE;}
if (!function_exists('ftp_chmod'))
{
if(!@ftp_site($this->conn_id, sprintf('CHMOD %o %s', $chmod, $file)))
{
$this->errors[] = "Unable to modify permissions";
$this->close_connection();
return FALSE;
}
}
else
{
if(!@ftp_chmod($this->conn_id, $chmod, $file))
{
$this->errors[] = "Unable to modify permissions";
$this->close_connection();
return FALSE;
}
}
$this->close_connection();
return TRUE;
}
/**
* Get the size in bytes of a remote file
* Can be used to check if a file exists
*
* @return bool|int FALSE if file doesn't exist or the number of bytes
* @access public
* @param string $filename The remote file to check
*/
function file_size($filename)
{
if(!$this->open_connection()){return FALSE;}
$file_size = @ftp_size($this->conn_id, $filename);
if(!$file_size or $file_size == -1)
{
$this->errors[] = "Unable to find remote file";
$this->close_connection();
return FALSE;
}
$this->close_connection();
return $file_size;
}
/**
* Checks whether a directory exists by trying to navigate to it
*
* @return bool
* @access public
* @param string $dir The directory to check
*/
function dir_exists($dir)
{
if(!$this->open_connection()){return FALSE;}
if(!@ftp_chdir($this->conn_id, $dir))
{
$this->close_connection();
return FALSE;
}
$this->close_connection();
return TRUE;
}
/**
* Returns the contents of a directory
*
* @return array|bool An array of files or a FALSE on error
* @access public
* @param string $dir The directory to read
*/
function dir_contents($dir)
{
$this->open_connection();
$f = @ftp_nlist($this->conn_id, $dir);
if(empty($f))
{
$this->errors[] = "Unable to read remote directory";
$this->close_connection();
return FALSE;
}
$this->close_connection();
return $f;
}
/**
* Attempts to open a connection to the remote server and authenticate the user
* Also sets the connection mode
*
* @return bool
* @access private
*/
private function open_connection()
{
if(!$conn_id = @ftp_connect($this->server, $this->port))
{
$this->errors[] = "Unable to connect to remote server";
}
if(!@ftp_login($conn_id, $this->username, $this->password))
{
$this->errors[] = "Connected to server but unable to authenticate user";
}
if(!@ftp_pasv($conn_id, $this->passive))
{
$this->errors[] = "Unable to set passive mode";
}
if(empty($this->errors))
{
$this->conn_id = $conn_id;
return TRUE;
}
return FALSE;
}
/**
* Attempts to close the connection
*
* @return bool
* @access private
*/
function close_connection()
{
if(!@ftp_close($this->conn_id)){return FALSE;}
$this->conn_id = "";
return TRUE;
}
}
?>