/
ftp.php
75 lines (60 loc) · 1.68 KB
/
ftp.php
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
<?
class knj_ftp{
function __construct($args = array()){
$this->args = $args;
if (!$this->args["port"]){
$this->args["port"] = 21;
}
}
function connect(){
$this->ftp = ftp_connect($this->args["host"], $this->args["port"]);
if (!$this->ftp){
throw new exception("Could not connect.");
}
if (!ftp_login($this->ftp, $this->args["user"], $this->args["passwd"])){
throw new exception("Could not log in.");
}
if (!array_key_exists("passive", $this->args) or $this->args["passive"]){
if (!ftp_pasv($this->ftp, true)){
throw new exception("Could not enable passive mode.");
}
}else{
if (!ftp_pasv($this->ftp, false)){
throw new exception("Could not disable passive mode.");
}
}
}
function put($args){
if (!file_exists($args["file"])){
throw new exception("File does not exist.");
}
if (!ftp_put($this->ftp, $args["path"], $args["file"], FTP_BINARY)){
if ($this->args["reconnect_on_error_and_try_again"]){
$this->connect();
if (!ftp_put($this->ftp, $args["path"], $args["file"], FTP_BINARY)){
throw new exception("Could not transfer file: " . $err["message"]);
}
}else{
throw new exception("Could not transfer file: " . $err["message"]);
}
}
}
function mkdir($args){
if (!is_array($args)){
throw new exception("Argument was not an array.");
}
if (strlen(trim($args["path"])) <= 0){
throw new exception("No path was given.");
}
if (!ftp_mkdir($this->ftp, $args["path"])){
throw new exception("Could not create dir: " . $args["path"]);
}
}
function exists($path){
$nlist = ftp_nlist($this->ftp, $path);
if (!is_array($nlist)){
return false;
}
return true;
}
}