8 * @author Shay Anderson 05.11
9 * @link shayanderson.com
10 * @license http://www.gnu.org/licenses/gpl.html GPL License
11 * SFTP is free software and is distributed WITHOUT ANY WARRANTY
13 namespace Utility\Remote;
52 private $_timeout = 90;
69 * FTP passive mode flag
73 public $passive = false;
76 * SSL-FTP connection flag
83 * System type of FTP server
85 * @var string $system_type
90 * Initialize connection params
94 * @param string $password
96 * @param int $timeout (seconds)
98 public function __construct($host = null, $user = null, $password = null, $port = 21, $timeout = 90) {
100 $this->_user = $user;
101 $this->_pwd = $password;
102 $this->_port = (int)$port;
103 $this->_timeout = (int)$timeout;
107 * Auto close connection
109 public function __destruct() {
114 * Change currect directory on FTP server
116 * @param string $directory
119 public function cd($directory = null) {
120 // attempt to change directory
121 if(ftp_chdir($this->_stream, $directory)) {
126 $this->error = "Failed to change directory to \"{$directory}\"";
132 * Set file permissions
134 * @param int $permissions (ex: 0644)
135 * @param string $remote_file
138 public function chmod($permissions = 0, $remote_file = null) {
140 if(ftp_chmod($this->_stream, $permissions, $remote_file)) {
145 $this->error = "Failed to set file permissions for \"{$remote_file}\"";
151 * Close FTP connection
153 public function close() {
154 // check for valid FTP stream
156 // close FTP connection
157 ftp_close($this->_stream);
160 $this->_stream = false;
165 * Connect to FTP server
169 public function connect() {
170 // check if non-SSL connection
172 // attempt connection
173 if(!$this->_stream = ftp_connect($this->_host, $this->_port, $this->_timeout)) {
175 $this->error = "Failed to connect to {$this->_host}";
179 } elseif(function_exists("ftp_ssl_connect")) {
180 // attempt SSL connection
181 if(!$this->_stream = ftp_ssl_connect($this->_host, $this->_port, $this->_timeout)) {
183 $this->error = "Failed to connect to {$this->_host} (SSL connection)";
186 // invalid connection type
188 $this->error = "Failed to connect to {$this->_host} (invalid connection type)";
193 if(ftp_login($this->_stream, $this->_user, $this->_pwd)) {
195 ftp_pasv($this->_stream, (bool)$this->passive);
198 $this->system_type = ftp_systype($this->_stream);
200 // connection successful
204 $this->error = "Failed to connect to {$this->_host} (login failed)";
210 * Delete file on FTP server
212 * @param string $remote_file
215 public function delete($remote_file = null) {
216 // attempt to delete file
217 if(ftp_delete($this->_stream, $remote_file)) {
222 $this->error = "Failed to delete file \"{$remote_file}\"";
228 * Download file from server
230 * @param string $remote_file
231 * @param string $local_file
235 public function get($remote_file = null, $local_file = null, $mode = FTP_ASCII) {
237 if(ftp_get($this->_stream, $local_file, $remote_file, $mode)) {
242 $this->error = "Failed to download file \"{$remote_file}\"";
248 * Get list of files/directories in directory
250 * @param string $directory
253 public function ls($directory = null) {
256 // attempt to get list
257 if($list = ftp_nlist($this->_stream, $directory)) {
262 $this->error = "Failed to get directory list";
268 * Create directory on FTP server
270 * @param string $directory
273 public function mkdir($directory = null) {
274 // attempt to create dir
275 if(ftp_mkdir($this->_stream, $directory)) {
280 $this->error = "Failed to create directory \"{$directory}\"";
286 * Upload file to server
288 * @param string $local_path
289 * @param string $remote_file_path
293 public function put($local_file = null, $remote_file = null, $mode = FTP_ASCII) {
294 // attempt to upload file
295 if(ftp_put($this->_stream, $remote_file, $local_file, $mode)) {
300 $this->error = "Failed to upload file \"{$local_file}\"";
306 * Get current directory
310 public function pwd() {
311 return ftp_pwd($this->_stream);
315 * Rename file on FTP server
317 * @param string $old_name
318 * @param string $new_name
321 public function rename($old_name = null, $new_name = null) {
323 if(ftp_rename($this->_stream, $old_name, $new_name)) {
328 $this->error = "Failed to rename file \"{$old_name}\"";
334 * Remove directory on FTP server
336 * @param string $directory
339 public function rmdir($directory = null) {
340 // attempt remove dir
341 if(ftp_rmdir($this->_stream, $directory)) {
346 $this->error = "Failed to remove directory \"{$directory}\"";