• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webeweb / core-library / 5185700055

pending completion
5185700055

push

github

webeweb
Update CHANGELOG

48909 of 49052 relevant lines covered (99.71%)

48.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.9
/src/ftp/Client/FtpClient.php
1
<?php
2

3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace WBW\Library\Ftp\Client;
13

14
use WBW\Library\Ftp\Exception\FtpException;
15
use WBW\Library\Security\Authenticator;
16
use WBW\Library\Types\Helper\StringHelper;
17

18
/**
19
 * FTP client.
20
 *
21
 * @author webeweb <https://github.com/webeweb>
22
 * @package WBW\Library\Ftp\Client
23
 */
24
class FtpClient extends AbstractClient {
25

26
    /**
27
     * Constructor.
28
     *
29
     * @param Authenticator $authenticator The authenticator.
30
     */
31
    public function __construct(Authenticator $authenticator) {
32
        parent::__construct($authenticator);
231✔
33

34
        $this->getAuthenticator()->setScheme("ftp");
231✔
35
        if (null === $this->getAuthenticator()->getPort()) {
231✔
36
            $this->getAuthenticator()->setPort(21);
210✔
37
        }
38
    }
66✔
39

40
    /**
41
     * Change to the parent directory.
42
     *
43
     * @return FtpClient Returns this FTP client.
44
     * @throws FtpException Throws an FTP exception if an error occurs.
45
     */
46
    public function cdup(): FtpClient {
47

48
        if (false === @ftp_cdup($this->getConnection())) {
7✔
49
            throw $this->newFtpException("ftp_cdup failed");
×
50
        }
51

52
        return $this;
7✔
53
    }
54

55
    /**
56
     * Change the current directory on a FTP server.
57
     *
58
     * @param string $directory The directory.
59
     * @return FtpClient Returns this FTP client.
60
     * @throws FtpException Throws an FTP exception if an error occurs.
61
     */
62
    public function chdir(string $directory): FtpClient {
63

64
        if (false === @ftp_chdir($this->getConnection(), $directory)) {
14✔
65
            throw $this->newFtpException("ftp_chdir failed: [$directory]");
7✔
66
        }
67

68
        return $this;
7✔
69
    }
70

71
    /**
72
     * Set permissions on a file via FTP.
73
     *
74
     * @param int $mode The mode.
75
     * @param string $filename The filename.
76
     * @return FtpClient Returns this FTP client.
77
     * @throws FtpException Throws an FTP exception if an error occurs.
78
     */
79
    public function chmod(int $mode, string $filename): FtpClient {
80

81
        if (false === @ftp_chmod($this->getConnection(), $mode, $filename)) {
7✔
82
            throw $this->newFtpException("ftp_chmod failed: [$mode, $filename]");
7✔
83
        }
84

85
        return $this;
×
86
    }
87

88
    /**
89
     * Close an FTP connection.
90
     *
91
     * @return FtpClient Returns this FTP client.
92
     * @throws FtpException Throws an FTP exception if an error occurs.
93
     */
94
    public function close(): FtpClient {
95

96
        if (null === $this->getConnection() || false === $this->getConnection()) {
217✔
97
            return $this;
14✔
98
        }
99

100
        if (false === @ftp_close($this->getConnection())) {
203✔
101
            throw $this->newFtpException("ftp_close failed");
4✔
102
        }
103

104
        return $this;
199✔
105
    }
106

107
    /**
108
     * Open an FTP connection.
109
     *
110
     * @param int $timeout The timeout.
111
     * @return FtpClient Returns this FTP client.
112
     * @throws FtpException Throws an FTP exception if an error occurs.
113
     */
114
    public function connect(int $timeout = 90): FtpClient {
115

116
        $host = $this->getAuthenticator()->getHostname();
210✔
117
        $port = $this->getAuthenticator()->getPort();
210✔
118

119
        $this->setConnection(@ftp_connect($host, $port, $timeout));
210✔
120
        if (false === $this->getConnection()) {
210✔
121
            throw $this->newFtpException("ftp_connect failed: [$host, $port, $timeout]");
7✔
122
        }
123

124
        return $this;
210✔
125
    }
126

127
    /**
128
     * Delete a file on the FTP server.
129
     *
130
     * @param string $path The path.
131
     * @return FtpClient Returns this FTP client.
132
     * @throws FtpException Throws an FTP exception if an error occurs.
133
     */
134
    public function delete(string $path): FtpClient {
135

136
        if (false === @ftp_delete($this->getConnection(), $path)) {
7✔
137
            throw $this->newFtpException("ftp_delete failed: [$path]");
7✔
138
        }
139

140
        return $this;
×
141
    }
142

143
    /**
144
     * Download a file from the FTP server and saves to an open file.
145
     *
146
     * @param resource $localStream The local stream.
147
     * @param string $remoteFile The remote file.
148
     * @param int $mode The mode.
149
     * @param int $resumePos The resume position.
150
     * @return FtpClient Returns this FTP client.
151
     * @throws FtpException Throws an FTP exception if an error occurs.
152
     */
153
    public function fget($localStream, string $remoteFile, int $mode = FTP_BINARY, int $resumePos = 0): FtpClient {
154

155
        if (false === @ftp_fget($this->getConnection(), $localStream, $remoteFile, $mode, $resumePos)) {
14✔
156
            throw $this->newFtpException("ftp_fget failed: [$localStream, $remoteFile, $mode, $resumePos]");
7✔
157
        }
158

159
        return $this;
7✔
160
    }
161

162
    /**
163
     * Upload from an open file to the FTP server.
164
     *
165
     * @param string $remoteFile The remote file.
166
     * @param resource $localStream The local stream.
167
     * @param int $mode The mode.
168
     * @param int $startPos The start position.
169
     * @return FtpClient Returns this FTP client.
170
     * @throws FtpException Throws an FTP exception if an error occurs.
171
     */
172
    public function fput(string $remoteFile, $localStream, int $mode = FTP_BINARY, int $startPos = 0): FtpClient {
173

174
        if (false === @ftp_fput($this->getConnection(), $remoteFile, $localStream, $mode, $startPos)) {
7✔
175
            throw $this->newFtpException("ftp_fput failed: [$remoteFile, $localStream, $mode, $startPos]");
7✔
176
        }
177

178
        return $this;
×
179
    }
180

181
    /**
182
     * Download a file from the FTP server.
183
     *
184
     * @param string $localFile The local file.
185
     * @param string $remoteFile The remote file.
186
     * @param int $mode The mode.
187
     * @param int $resumePos The resume position.
188
     * @return FtpClient Returns this FTP client.
189
     * @throws FtpException Throws an FTP exception if an error occurs.
190
     */
191
    public function get(string $localFile, string $remoteFile, int $mode = FTP_BINARY, int $resumePos = 0): FtpClient {
192

193
        if (false === @ftp_get($this->getConnection(), $localFile, $remoteFile, $mode, $resumePos)) {
14✔
194
            throw $this->newFtpException("ftp_get failed: [$localFile, $remoteFile, $mode, $resumePos]");
7✔
195
        }
196

197
        return $this;
7✔
198
    }
199

200
    /**
201
     * Log in to an FTP connection.
202
     *
203
     * @return FtpClient Returns this FTP client.
204
     * @throws FtpException Throws an FTP exception if an error occurs.
205
     */
206
    public function login(): FtpClient {
207

208
        $username = $this->getAuthenticator()->getPasswordAuthentication()->getUsername();
210✔
209
        $password = $this->getAuthenticator()->getPasswordAuthentication()->getPassword();
210✔
210

211
        if (false === @ftp_login($this->getConnection(), $username, $password)) {
210✔
212
            throw $this->newFtpException("ftp_login failed: [$username]");
7✔
213
        }
214

215
        return $this;
210✔
216
    }
217

218
    /**
219
     * Return the last modified time of the given file.
220
     *
221
     * @param string $remoteFile The remote file.
222
     * @return int Returns the last modified time of the given file.
223
     * @throws FtpException Throws an FTP exception if an error occurs.
224
     */
225
    public function mdtm(string $remoteFile): int {
226

227
        $result = @ftp_mdtm($this->getConnection(), $remoteFile);
14✔
228
        if (-1 === $result) {
14✔
229
            throw $this->newFtpException("ftp_mdtm failed: [$remoteFile]");
7✔
230
        }
231

232
        return $result;
7✔
233
    }
234

235
    /**
236
     * Create a directory.
237
     *
238
     * @param string $directory The directory.
239
     * @return FtpClient Returns this FTP client.
240
     * @throws FtpException Throws an FTP exception if an error occurs.
241
     */
242
    public function mkdir(string $directory): FtpClient {
243

244
        if (false === @ftp_mkdir($this->getConnection(), $directory)) {
7✔
245
            throw $this->newFtpException("ftp_mkdir failed: [$directory]");
7✔
246
        }
247

248
        return $this;
×
249
    }
250

251
    /**
252
     * Retrieve a file from the FTP server and writes it to an open file (non-blocking).
253
     *
254
     * @param resource $localStream The local stream.
255
     * @param string $remoteFile The remote file.
256
     * @param int $mode The mode.
257
     * @param int $resumePos The resume position.
258
     * @return int|null
259
     */
260
    public function nbFget($localStream, string $remoteFile, int $mode = FTP_BINARY, int $resumePos = 0): ?int {
261
        return @ftp_nb_fget($this->getConnection(), $localStream, $remoteFile, $mode, $resumePos);
7✔
262
    }
263

264
    /**
265
     * Store a file from an open file to the FTP server (non-blocking).
266
     *
267
     * @param string $remoteFile The remote file.
268
     * @param resource $localStream The local stream.
269
     * @param int $mode The mode.
270
     * @param int $startPos The start position.
271
     * @return int
272
     */
273
    public function nbFput(string $remoteFile, $localStream, int $mode = FTP_BINARY, int $startPos = 0): int {
274
        return @ftp_nb_fput($this->getConnection(), $remoteFile, $localStream, $mode, $startPos);
7✔
275
    }
276

277
    /**
278
     * Retrieve a file from the FTP server and writes it to a local file (non-blocking).
279
     *
280
     * @param string $localFile The local file.
281
     * @param string $remoteFile The remote file.
282
     * @param int $mode The mode.
283
     * @param int $resumePos The resume position.
284
     * @return int
285
     */
286
    public function nbGet(string $localFile, string $remoteFile, int $mode = FTP_BINARY, int $resumePos = 0): int {
287
        return @ftp_nb_get($this->getConnection(), $localFile, $remoteFile, $mode, $resumePos);
7✔
288
    }
289

290
    /**
291
     * Store a file from an open file to the FTP server (non-blocking).
292
     *
293
     * @param string $remoteFile The remote file.
294
     * @param string $localFile The local file.
295
     * @param int $mode The mode.
296
     * @param int $startPos The start position.
297
     * @return int
298
     */
299
    public function nbPut(string $remoteFile, string $localFile, int $mode = FTP_BINARY, int $startPos = 0): int {
300
        return @ftp_nb_put($this->getConnection(), $remoteFile, $localFile, $mode, $startPos);
7✔
301
    }
302

303
    /**
304
     * Return a list of files in the given directory.
305
     *
306
     * @param string $directory The directory.
307
     * @return string[] Returns a list of files in the given directory.
308
     * @throws FtpException Throws an FTP exception if an error occurs.
309
     */
310
    public function nlist(string $directory): array {
311

312
        $result = @ftp_nlist($this->getConnection(), $directory);
14✔
313
        if (false === $result) {
14✔
314
            throw $this->newFtpException("ftp_nlist failed: [$directory]");
7✔
315
        }
316

317
        return $result;
7✔
318
    }
319

320
    /**
321
     * Turn passive mode on or off.
322
     *
323
     * @param bool $pasv The passive mode.
324
     * @return FtpClient Returns this FTP client.
325
     * @throws FtpException Throws an FTP exception if an error occurs.
326
     */
327
    public function pasv(bool $pasv): FtpClient {
328

329
        if (false === @ftp_pasv($this->getConnection(), $pasv)) {
210✔
330
            throw $this->newFtpException("ftp_pasv failed: [" . StringHelper::parseBoolean($pasv) . "]");
×
331
        }
332

333
        return $this;
210✔
334
    }
335

336
    /**
337
     * Upload a file to the FTP server.
338
     *
339
     * @param string $localFile The local file.
340
     * @param string $remoteFile The remote file.
341
     * @param int $mode The mode.
342
     * @param int $startPos The start position.
343
     * @return FtpClient Returns this FTP client.
344
     * @throws FtpException Throws an FTP exception if an error occurs.
345
     */
346
    public function put(string $localFile, string $remoteFile, int $mode = FTP_BINARY, int $startPos = 0): FtpClient {
347

348
        if (false === @ftp_put($this->getConnection(), $remoteFile, $localFile, $mode, $startPos)) {
7✔
349
            throw $this->newFtpException("ftp_put failed: [$remoteFile, $localFile, $mode, $startPos]");
7✔
350
        }
351

352
        return $this;
×
353
    }
354

355
    /**
356
     * Return the current directory name.
357
     *
358
     * @return string Returns the current directory name.
359
     * @throws FtpException Throws an FTP exception if an error occurs.
360
     */
361
    public function pwd(): string {
362

363
        $result = @ftp_pwd($this->getConnection());
7✔
364
        if (false === $result) {
7✔
365
            throw $this->newFtpException("ftp_pwd failed");
×
366
        }
367

368
        return $result;
7✔
369
    }
370

371
    /**
372
     * Return a detailed list of files in the given directory.
373
     *
374
     * @param string $directory The directory.
375
     * @param bool $recursive Recursive ?
376
     * @return array Returns a detailed list of files in the given directory.
377
     * @throws FtpException Throws an FTP exception if an error occurs.
378
     */
379
    public function rawList(string $directory, bool $recursive = false): array {
380

381
        $result = @ftp_rawList($this->getConnection(), $directory, $recursive);
14✔
382
        if (false === $result) {
14✔
383
            throw $this->newFtpException("ftp_rawList failed: [$directory, " . StringHelper::parseBoolean($recursive) . "]");
7✔
384
        }
385

386
        return $result;
7✔
387
    }
388

389
    /**
390
     * Rename a file or a directory on the FTP server.
391
     *
392
     * @param string $oldName The old name.
393
     * @param string $newName The new name.
394
     * @return FtpClient Returns this FTP client.
395
     * @throws FtpException Throws an FTP exception if an error occurs.
396
     */
397
    public function rename(string $oldName, string $newName): FtpClient {
398

399
        if (false === @ftp_rename($this->getConnection(), $oldName, $newName)) {
7✔
400
            throw $this->newFtpException("ftp_rename failed: [$oldName, $newName]");
7✔
401
        }
402

403
        return $this;
×
404
    }
405

406
    /**
407
     * Remove a directory.
408
     *
409
     * @param string $directory The directory.
410
     * @return FtpClient Returns this FTP client.
411
     * @throws FtpException Throws an FTP exception if an error occurs.
412
     */
413
    public function rmdir(string $directory): FtpClient {
414

415
        if (false === @ftp_rmdir($this->getConnection(), $directory)) {
7✔
416
            throw $this->newFtpException("ftp_rmdir failed: [$directory]");
7✔
417
        }
418

419
        return $this;
×
420
    }
421

422
    /**
423
     * Return the size of the given file.
424
     *
425
     * @param string $remoteFile The remote file.
426
     * @return int Returns the size of the given file.
427
     * @throws FtpException Throws an FTP exception if an error occurs.
428
     */
429
    public function size(string $remoteFile): int {
430

431
        $result = @ftp_size($this->getConnection(), $remoteFile);
14✔
432
        if (-1 === $result) {
14✔
433
            throw $this->newFtpException("ftp_size failed: [$remoteFile]");
7✔
434
        }
435

436
        return $result;
7✔
437
    }
438

439
    /**
440
     * Return the system type identifier of the remote FTP server.
441
     *
442
     * @return string Returns the system type identifier of the remote FTP server.
443
     * @throws FtpException Throws an FTP exception if an error occurs.
444
     */
445
    public function systype(): string {
446

447
        $result = @ftp_systype($this->getConnection());
7✔
448
        if (false === $result) {
7✔
449
            throw $this->newFtpException("ftp_systype failed");
×
450
        }
451

452
        return $result;
7✔
453
    }
454
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc