namibia
df0489e1eeeeab5a9bd44e1d84fce49924fe1bac
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
<?php /** * Simple FTP Class * * @package Sftp * @name Sftp * @version 1.0 * @author Shay Anderson 05.11 * @link shayanderson.com * @license http://www.gnu.org/licenses/gpl.html GPL License * SFTP is free software and is distributed WITHOUT ANY WARRANTY */ namespace Utility\Remote; class Sftp { /** * FTP host * * @var string $_host */ private $_host; /** * FTP port * * @var int $_port */ private $_port = 21; /** * FTP password * * @var string $_pwd */ private $_pwd; /** * FTP stream * * @var resource $_id */ private $_stream; /** * FTP timeout * * @var int $_timeout */ private $_timeout = 90; /** * FTP user * * @var string $_user */ private $_user; /** * Last error * * @var string $error */ public $error; /** * FTP passive mode flag * * @var bool $passive */ public $passive = false; /** * SSL-FTP connection flag * * @var bool $ssl */ public $ssl = false; /** * System type of FTP server * * @var string $system_type */ public $system_type; /** * Initialize connection params * * @param string $host * @param string $user * @param string $password * @param int $port * @param int $timeout (seconds) */ public function __construct($host = null, $user = null, $password = null, $port = 21, $timeout = 90) { $this->_host = $host; $this->_user = $user; $this->_pwd = $password; $this->_port = (int)$port; $this->_timeout = (int)$timeout; } /** * Auto close connection */ public function __destruct() { $this->close(); } /** * Change currect directory on FTP server * * @param string $directory * @return bool */ public function cd($directory = null) { // attempt to change directory if(ftp_chdir($this->_stream, $directory)) { // success return true; // fail } else { $this->error = "Failed to change directory to \"{$directory}\""; return false; } } /** * Set file permissions * * @param int $permissions (ex: 0644) * @param string $remote_file * @return false */ public function chmod($permissions = 0, $remote_file = null) { // attempt chmod if(ftp_chmod($this->_stream, $permissions, $remote_file)) { // success return true; // failed } else { $this->error = "Failed to set file permissions for \"{$remote_file}\""; return false; } } /** * Close FTP connection */ public function close() { // check for valid FTP stream if($this->_stream) { // close FTP connection ftp_close($this->_stream); // reset stream $this->_stream = false; } } /** * Connect to FTP server * * @return bool */ public function connect() { // check if non-SSL connection if(!$this->ssl) { // attempt connection if(!$this->_stream = ftp_connect($this->_host, $this->_port, $this->_timeout)) { // set last error $this->error = "Failed to connect to {$this->_host}"; return false; } // SSL connection } elseif(function_exists("ftp_ssl_connect")) { // attempt SSL connection if(!$this->_stream = ftp_ssl_connect($this->_host, $this->_port, $this->_timeout)) { // set last error $this->error = "Failed to connect to {$this->_host} (SSL connection)"; return false; } // invalid connection type } else { $this->error = "Failed to connect to {$this->_host} (invalid connection type)"; return false; } // attempt login if(ftp_login($this->_stream, $this->_user, $this->_pwd)) { // set passive mode ftp_pasv($this->_stream, (bool)$this->passive); // set system type $this->system_type = ftp_systype($this->_stream); // connection successful return true; // login failed } else { $this->error = "Failed to connect to {$this->_host} (login failed)"; return false; } } /** * Delete file on FTP server * * @param string $remote_file * @return bool */ public function delete($remote_file = null) { // attempt to delete file if(ftp_delete($this->_stream, $remote_file)) { // success return true; // fail } else { $this->error = "Failed to delete file \"{$remote_file}\""; return false; } } /** * Download file from server * * @param string $remote_file * @param string $local_file * @param int $mode * @return bool */ public function get($remote_file = null, $local_file = null, $mode = FTP_ASCII) { // attempt download if(ftp_get($this->_stream, $local_file, $remote_file, $mode)) { // success return true; // download failed } else { $this->error = "Failed to download file \"{$remote_file}\""; return false; } } /** * Get list of files/directories in directory * * @param string $directory * @return array */ public function ls($directory = null) { $list = array(); // attempt to get list if($list = ftp_nlist($this->_stream, $directory)) { // success return $list; // fail } else { $this->error = "Failed to get directory list"; return array(); } } /** * Create directory on FTP server * * @param string $directory * @return bool */ public function mkdir($directory = null) { // attempt to create dir if(ftp_mkdir($this->_stream, $directory)) { // success return true; // fail } else { $this->error = "Failed to create directory \"{$directory}\""; return false; } } /** * Upload file to server * * @param string $local_path * @param string $remote_file_path * @param int $mode * @return bool */ public function put($local_file = null, $remote_file = null, $mode = FTP_ASCII) { // attempt to upload file if(ftp_put($this->_stream, $remote_file, $local_file, $mode)) { // success return true; // upload failed } else { $this->error = "Failed to upload file \"{$local_file}\""; return false; } } /** * Get current directory * * @return string */ public function pwd() { return ftp_pwd($this->_stream); } /** * Rename file on FTP server * * @param string $old_name * @param string $new_name * @return bool */ public function rename($old_name = null, $new_name = null) { // attempt rename if(ftp_rename($this->_stream, $old_name, $new_name)) { // success return true; // fail } else { $this->error = "Failed to rename file \"{$old_name}\""; return false; } } /** * Remove directory on FTP server * * @param string $directory * @return bool */ public function rmdir($directory = null) { // attempt remove dir if(ftp_rmdir($this->_stream, $directory)) { // success return true; // fail } else { $this->error = "Failed to remove directory \"{$directory}\""; return false; } } } |
Commits for namibiamodule/Utility/src/Utility/Remote/Sftp.php
Revision | Author | Commited | Message |
---|---|---|---|
df0489 ... | Mark | Fri 14 Oct, 2016 10:01:00 +0000 | initial commit |