-
Notifications
You must be signed in to change notification settings - Fork 0
/
FTP Connection Class.php
205 lines (137 loc) · 5.9 KB
/
FTP Connection Class.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
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
<?php
class FTP {
private $toIgnore;
private $server;
private $username;
private $password;
private $connection;
private $serverContent;
private $serverContentUpdated;
private $pwd;
private $pwdChanged;
function __construct($_server, $_username, $_password){
$this->toIgnore = ($_server == "demo" || $_username == "demo" || $_password == "demo") ? true : false;
$this->server = $_server;
$this->username = $_username;
$this->password = $_password;
$this->pwd = "";
$this->pwdChanged = true;
$this->serverContentUpdated = true;
}
private function connect() {
if($this->toIgnore) { return false; }
try {
$ftpConn = ftp_connect($this->server);
if (false === $ftpConn) {
throw new Exception('Unable to connect');
return false;
}
$loggedIn = ftp_login($ftpConn, $this->username, $this->password);
if (false === $loggedIn) {
throw new Exception('Unable to log in');
return false;
}
} catch (Exception $error) {
echo "Failure (" . $this->server . "): " . $error->getMessage();
return false;
}
$this->connection = $ftpConn;
return true;
}
private function disconnect() {
if(isset($this->connection) && $this->connection != null)
ftp_close($this->connection);
}
function removeExtension($name) {
return substr($name,0,-strpos(strrev($name),".") - 1);
}
function getExtension($name) {
return pathinfo($name, PATHINFO_EXTENSION);
}
function deleteFile($fileName, $filePath = null) {
if($this->toIgnore) { return false; }
if(!$this->connect()) { return; }
if($this->pwd != $filePath && $filePath != null) {
$this->pwd = $filePath;
$this->pwdChanged = true;
}
$this->serverContentUpdated = ftp_delete($this->connection, $this->pwd . $fileName);
echo $this->serverContentUpdated ;
$this->disconnect();
}
function uploadFile($fileName, $fileOriginalPath, $fileDestinyPath = null, $deleteOriginal = false) {
if($this->toIgnore) { return false; }
if(!$this->connect()) { return; }
$ext = '.' . $this->getExtension($fileOriginalPath);
if($this->getExtension($fileName) != null)
$fileName = $this->removeExtension($fileName);
if($this->pwd != $fileDestinyPath && $fileDestinyPath != null) {
$this->pwd = $fileDestinyPath;
$this->pwdChanged = true;
}
ftp_pasv($this->connection, true);
$feedback = ftp_nb_put($this->connection, $this->pwd . $fileName . $ext, $fileOriginalPath, FTP_BINARY);
while ($feedback == FTP_MOREDATA) {
$feedback = ftp_nb_continue($this->connection);
}
if ($feedback != FTP_FINISHED || $feedback == FTP_FAILED) {
$deleteOriginal = false;
echo("Upload failed (" . $feedback . "): " . $fileName . "<br>");
}
if(!$this->pwdChanged)
$this->serverContentUpdated = true;
echo("Upload finished: " . $fileName . $ext . "<br>");
if($deleteOriginal) {
unlink("../tempImages/" . $fileName . $ext); //Not dynamic
echo("File deleted: " . $fileName . $ext . "<br><br>");
}
ftp_pasv($this->connection, false);
$this->disconnect();
//OUTRO MÉTODO
/*$ext = '.' . $this->getExtension($fileOriginalPath);
//Exemplo: fopen("ftp://[email protected]:[email protected]/imagens/novaImg.jpg", "wb")
$destination = fopen("ftp://" . $this->username . ":" . $this->password . "@" . str_replace("ftp.", "", $this->server) . "/" . $filePath . $fileName . $ext, "wb");
$source = file_get_contents($fileOriginalPath);
fwrite($destination, $source, strlen($source));
fclose($destination);
*/
}
function createFile($fileName, $fileContent, $filePath = null) { //fileName incluir extenção
if($this->toIgnore) { return false; }
if(!$this->connect()) { return; }
if($this->pwd != $filePath && $filePath != null) {
$this->pwd = $filePath;
$this->pwdChanged = true;
}
$destination = fopen("ftp://" . $this->username . ":" . $this->password . "@" . str_replace("ftp.", "", $this->server) . "/" . $this->pwd . $fileName . $ext, "wb");
fwrite($destination, $fileContent, strlen($fileContent));
fclose($destination);
$this->disconnect();
}
function renameFile($oldName, $newName) { //Incluir "path" se fôr o caso e extenção, funciona também para mover o ficheiro
if($this->toIgnore) { return false; }
if(!$this->connect()) { return; }
ftp_rename($this->connection, $oldName, $newName);
$this->disconnect();
}
function fileExists($fileName, $directory = null) {
if($this->toIgnore) { return false; }
if(in_array(explode("-",$fileName)[0], ['24']))
return true;
if($this->pwd != $directory && $directory != null) {
$this->pwd = $directory;
$this->pwdChanged = true;
}
if($this->pwdChanged || $this->serverContentUpdated) {
if(!$this->connect()) { return false; }
ftp_pasv($this->connection, true);
$this->serverContent = ftp_nlist($this->connection, $this->pwd);
ftp_pasv($this->connection, false);
$this->disconnect();
$this->pwdChanged = false;
$this->serverContentUpdated = false;
}
return in_array($this->pwd . $fileName, $this->serverContent);
}
}
?>