|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Phant\Client\Service; |
| 6 | + |
| 7 | +use FTP\Connection; |
| 8 | +use Phant\Client\Port\Ftp\Exception\FtpClientException; |
| 9 | +use Phant\Client\Port\Ftp\Exception\FtpConnectionException; |
| 10 | +use Phant\Client\Port\Ftp\Exception\FtpLoginException; |
| 11 | + |
| 12 | +/** |
| 13 | + * FTP Service for interacting with FTP servers. |
| 14 | + */ |
| 15 | +class Ftp implements \Phant\Client\Port\Ftp |
| 16 | +{ |
| 17 | + private Connection $connection; |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + string $host, |
| 21 | + string $username, |
| 22 | + string $password, |
| 23 | + int $port = 21, |
| 24 | + bool $passiveMode = false |
| 25 | + ) { |
| 26 | + $this->connection = $this->connect( |
| 27 | + host: $host, |
| 28 | + username: $username, |
| 29 | + password: $password, |
| 30 | + port: $port |
| 31 | + ); |
| 32 | + |
| 33 | + ftp_pasv($this->connection, $passiveMode); |
| 34 | + } |
| 35 | + |
| 36 | + private function connect(string $host, string $username, string $password, int $port): Connection |
| 37 | + { |
| 38 | + $ftp = ftp_connect($host, $port); |
| 39 | + if (!$ftp) { |
| 40 | + throw new FtpConnectionException($host); |
| 41 | + } |
| 42 | + |
| 43 | + $login = ftp_login($ftp, $username, $password); |
| 44 | + if (!$login) { |
| 45 | + throw new FtpLoginException($host); |
| 46 | + } |
| 47 | + |
| 48 | + return $ftp; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Retrieve a list of files in the specified directory. |
| 53 | + * @param string $path |
| 54 | + * @return array|bool |
| 55 | + * @throws FtpClientException |
| 56 | + */ |
| 57 | + public function listFiles(string $path): array |
| 58 | + { |
| 59 | + $list = ftp_nlist($this->connection, $path); |
| 60 | + if ($list === false) { |
| 61 | + throw new FtpClientException(error_get_last()['message'] ?? 'Unknown error during FTP listing'); |
| 62 | + } |
| 63 | + |
| 64 | + $files = array_filter($list, fn ($resource) => !is_dir($resource)); |
| 65 | + |
| 66 | + return $files; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Retrieve a list of folders in the specified directory. |
| 71 | + * @param string $path |
| 72 | + * @return array|bool |
| 73 | + * @throws FtpClientException |
| 74 | + */ |
| 75 | + public function listFolders(string $path): array |
| 76 | + { |
| 77 | + $list = ftp_nlist($this->connection, $path); |
| 78 | + if ($list === false) { |
| 79 | + throw new FtpClientException(error_get_last()['message'] ?? 'Unknown error during FTP listing'); |
| 80 | + } |
| 81 | + |
| 82 | + $folders = array_filter($list, fn ($resource) => is_dir($resource)); |
| 83 | + |
| 84 | + return $folders; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Retrieve a list of all resources in the specified directory. |
| 89 | + * @param string $path |
| 90 | + * @return array|bool |
| 91 | + * @throws FtpClientException |
| 92 | + */ |
| 93 | + public function listAll(string $path): array |
| 94 | + { |
| 95 | + $list = ftp_nlist($this->connection, $path); |
| 96 | + if ($list === false) { |
| 97 | + throw new FtpClientException(error_get_last()['message'] ?? 'Unknown error during FTP listing'); |
| 98 | + } |
| 99 | + |
| 100 | + if (false === $list) { |
| 101 | + return []; |
| 102 | + } |
| 103 | + |
| 104 | + return $list; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Download a file from the FTP server. You can specify a local directory to save the file. If no directory is provided, the file will be saved in the system's temporary directory. |
| 109 | + * @param string $remotePath |
| 110 | + * @param ?string $localDirectory |
| 111 | + * @return string |
| 112 | + * @throws FtpClientException |
| 113 | + */ |
| 114 | + public function download(string $remoteFilePath, ?string $localDirectory = null): string |
| 115 | + { |
| 116 | + if ($localDirectory) { |
| 117 | + $localFilePath = rtrim($localDirectory, '/') . '/' . basename($remoteFilePath); |
| 118 | + } else { |
| 119 | + $localFilePath = sys_get_temp_dir() . '/' . basename($remoteFilePath); |
| 120 | + } |
| 121 | + |
| 122 | + if (!is_dir(dirname($localFilePath))) { |
| 123 | + throw new FtpClientException("Local directory does not exist: " . dirname($localFilePath)); |
| 124 | + } |
| 125 | + |
| 126 | + if (!ftp_get($this->connection, $localFilePath, $remoteFilePath, FTP_BINARY)) { |
| 127 | + throw new FtpClientException("Failed to download file from FTP: {$remoteFilePath}. Error: " . (error_get_last()['message'] ?? 'Unknown error')); |
| 128 | + } |
| 129 | + |
| 130 | + return $localFilePath; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Delete a file from the FTP server. |
| 135 | + * @param string $path |
| 136 | + * @return bool |
| 137 | + * @throws FtpClientException |
| 138 | + */ |
| 139 | + public function delete(string $path): bool |
| 140 | + { |
| 141 | + if (!ftp_delete($this->connection, $path)) { |
| 142 | + throw new FtpClientException("Failed to delete file from FTP: {$path}"); |
| 143 | + } |
| 144 | + |
| 145 | + return true; |
| 146 | + } |
| 147 | + |
| 148 | + public function __destruct() |
| 149 | + { |
| 150 | + ftp_close($this->connection); |
| 151 | + } |
| 152 | + |
| 153 | + public function lala() |
| 154 | + { |
| 155 | + $ftpClient = new self( |
| 156 | + host: 'example.com', |
| 157 | + username: 'user', |
| 158 | + password: 'pass' |
| 159 | + ); |
| 160 | + |
| 161 | + $files = $ftpClient->listFiles('/path/to/directory'); |
| 162 | + $downloadedFile = $ftpClient->download('/path/to/remote/file.txt', '/local/directory'); |
| 163 | + } |
| 164 | +} |
0 commit comments