-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftp_requests.php
More file actions
159 lines (142 loc) · 3.82 KB
/
ftp_requests.php
File metadata and controls
159 lines (142 loc) · 3.82 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
<?php
/**
* @file Simple wrapper for cURL functions to transfer an ASCII file over FTP with implicit TLS.
*
* @author Ra Mänd <ram4nd@gmail.com>
* @link http://browse-tutorials.com/
*
* Example:
* require_once 'ftp_requests.php'
* $ftp = new ftp('username', 'password', 'server');
*/
class Ftp {
private $username;
private $password;
private $server;
private $path;
private $options;
private $ch;
private $url;
/**
* Setup and init connection.
*
* @var string $username
* @var string $password
* @var string $path
* @var number $port
*
* @throws Exception
*/
public function __construct($username, $password, $server, $path = '/', $port = 21) {
$this->username = $username;
$this->password = $password;
$this->server = $server;
// Set host/initial path.
$this->url = 'ftp://' . $server . $path;
$this->path = $path;
// Connection options.
$this->options = array(
CURLOPT_USERPWD => $username . ':' . $password,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_INFILE => null,
CURLOPT_INFILESIZE => -1,
CURLOPT_FTPSSLAUTH => CURLFTPAUTH_TLS,
CURLOPT_UPLOAD => false,
CURLOPT_PORT => $port,
CURLOPT_TIMEOUT => 30,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FTPLISTONLY => 0,
);
}
/**
* Return array of file names.
*
* @throws Exception
*
* @return array
*/
public function files() {
$this->_init();
if (!curl_setopt($this->ch, CURLOPT_URL, $this->url)) {
throw new Exception ('Could not set cURL directory: ' . $this->url);
}
curl_setopt($this->ch, CURLOPT_FTPLISTONLY, 1);
$result = curl_exec($this->ch) or die (curl_error($this->ch));
$files = explode("\n",trim($result));
if (count($files)) {
return $files;
}
else {
return array();
}
}
/**
* Download remote files content.
*
* @var string $file_name
*
* @throws Exception
*
* @return string
*/
public function download($file_name) {
$this->_init();
$file = tmpfile();
curl_setopt($this->ch, CURLOPT_URL, $this->url . $file_name);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_FILE, $file);
$result = curl_exec($this->ch);
if ($result === false) {
throw new Exception(curl_error($this->ch));
}
$file_contents = file_get_contents(stream_get_meta_data($file)['uri']);
fclose($file);
return $file_contents;
}
/**
* Delete remote file.
*
* @var string $file_name
*
* @throws Exception
*
* @return string
*/
public function delete($file_name) {
$this->_init();
curl_setopt($this->ch, CURLOPT_URL, $this->url);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_QUOTE, array('DELE ' . $this->path . $file_name));
$result = curl_exec($this->ch);
if ($result === false) {
throw new Exception(curl_error($this->ch));
}
return $result;
}
/**
* Initialise cURL handle.
*/
private function _init() {
// Setup connection.
$this->ch = curl_init();
// Check for successful connection.
if (!$this->ch) {
throw new Exception('Could not initialize cURL.');
}
// Set connection options, use foreach so useful errors can be caught
// instead of a generic "cannot set options" error with curl_setopt_array().
foreach ($this->options as $option_name => $option_value) {
if (!curl_setopt($this->ch, $option_name, $option_value)) {
throw new Exception(sprintf('Could not set cURL option: %s', $option_name));
}
}
}
/**
* Attempt to close cURL handle.
*/
public function __destruct() {
@curl_close($this->ch);
}
}