/
FTPClient.php.inc
executable file
·109 lines (100 loc) · 2.92 KB
/
FTPClient.php.inc
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
<?php
/**
* @brief FTP client object library
* @details Engine / Object Library
* @file engine/lib/object/FTPClient.php.inc
* @author CaMykS Team <camyks.contact@gmail.com>
* @version 1.0
* @date Creation: Mar 2018
* @date Modification: Apr 2018
* @copyright 2018 - 2018 CaMykS Team
* @note This program is distributed as is - WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* FSFile class.
* @author CaMykS Team <camyks.contact@gmail.com>
*/
final class FTPClient {
/**
* @var Resource $connection
* @brief Connection to server.
*/
private $connection;
/**
* Class constructor.
* @return void
*/
public function __construct() {
$this->connection = null;
}
/**
* Class destructor.
* @return void
*/
public function __destruct() {
if ($this->connection !== null)
$this->close();
}
/**
* Magic method to catch all FTP commands from PHP.
* @param string $method
* @param array $arguments
* @return mixed
*/
public function __call($method, $arguments) {
/* add php method prefix */
$function = 'ftp_' . $method;
if (function_exists($function)) {
/* adds connection as first argument */
array_unshift($arguments, $this->connection);
/* execute function */
return call_user_func_array($function, $arguments);
}
/* php function doesn't exist, return false */
return false;
}
/**
* Explicitly define connect method to get connection object.
* @param string $host
* @param boolean $ssl
* @param integer $port
* @param integer $timeout
* @return boolean success
*/
public function connect($host, $ssl=false, $port=21, $timeout=60) {
/* check if ftp is available */
if (!extension_loaded('ftp'))
return false;
if ($ssl) {
$this->connection = ftp_ssl_connect($host, $port, $timeout);
} else {
$this->connection = ftp_connect($host, $port, $timeout);
}
return ($this->connection !== false);
}
/**
* Explicitly define ssl_connect method.
* @param string $host
* @param integer $port
* @param integer $timeout
* @return boolean success
*/
public function ssl_connect($host, $port=21, $timeout=60) {
return $this->connect($host, true, $port, $timeout);
}
/* specific methods */
/**
* Write a file to the server directly from given content.
* @param string $remoteFilePath
* @param string $content
* @return boolean success
*/
public function write_content($remoteFilePath, $content) {
$temp = fopen('php://temp', 'w');
fwrite($temp, $content);
rewind($temp);
return $this->fput($remoteFilePath, $temp, FTP_BINARY);
}
}
?>