4 * @author Tjaart Viljoen
5 * @copyright 2013 Nirph Online
7 namespace Utility\Remote;
11 protected $_serverAddress;
12 protected $_connection;
16 protected $_timeout = 60;
20 * Set default parameters.
21 * @param string $serverAddress
22 * @param string $username
23 * @param string $password
25 function __construct($serverAddress, $username, $password, $timeout = 60, $verbose = false)
27 $this->_serverAddress = $serverAddress;
28 $this->_username = $username;
29 $this->_password = $password;
30 $this->_verbose = $verbose;
31 $this->_timeout = $timeout;
35 * Change remote FTP directory.
36 * @param unknown $directory
37 * @throws \ErrorException
40 public function changeDirectory($directory)
42 $this->_secureConnection();
43 $this->_output('Changing directory to "' . $directory . '"');
44 $result = ftp_chdir($this->_connection, $directory);
48 error_log('Couldn\'t change to FTP directory "' . $directory . '"');
49 throw new \Exception('Couldn\'t change to FTP directory "' . $directory . '"');
56 * Create a new remote directory.
57 * @param string $directory
61 public function createDirectory($directory)
63 $this->_secureConnection();
64 $this->_output('Creating directory "' . $directory . '"');
65 $result = ftp_mkdir($this->_connection, $directory);
68 $error = 'Couldn\'t create remote FTP directory "' . $directory . '"' ;
70 throw new \Exception($error);
77 * Connect to the remote server.
79 * @return \Utility\Remote\Ftp
81 public function connect()
83 $this->_connection = ftp_connect($this->_serverAddress);
85 if(false == $this->_connection)
87 throw new \Exception("Couldn't connect to FTP server: " . $this->_serverAddress );
89 $this->_output('Connected to "' . $this->_serverAddress . '"');
94 * Close the remote FTP server connection.
96 public function closeConnection()
98 $this->_output('Closing FTP connection');
99 ftp_close($this->_connection);
100 $this->_connection = null;
104 * Login to the remote FTP server.
106 * @return \Utility\Remote\Ftp
108 protected function _login()
110 $this->_output('Logging in to "' . $this->_serverAddress . '"');
111 $logingResult = ftp_login($this->_connection, $this->_username, $this->_password);
113 if(false == $logingResult)
115 $this->_output('Login failure');
116 $this->closeConnection();
117 throw new \Exception("Couldn't log into remote server, please check your credentials and try again");
119 $this->_output('Logged in to "' . $this->_serverAddress . '"');
124 * $return the content of a directory.
125 * Returns an empty array on error.
126 * @param string $directory
129 public function getDirectoryContents($directory = '.')
131 $this->_secureConnection();
132 $this->_output('Retrieving direcory contents for "' . $directory . '"');
133 $contents = ftp_nlist($this->_connection, '.');
135 if(false == $contents)
137 error_log('Couldn\'t retrieve directory contents for: ' . $this->_serverAddress . ', directory: "' . $directory . '"');
144 * Delete a remote Directory
145 * @example deleteRemoteDirectory('./relative-path-directory');
146 * @example deleteRemoteDirectory('/temp/absolute-path-directory');
147 * @param string $directory
150 public function deleteRemoteDirectory($directory)
152 $this->_secureConnection();
153 $this->_output('Deleting direcory "' . $directory . '"');
154 $result = ftp_rmdir($this->_connection, $directory);
158 error_log('Couldn\'t delete remote FTP directory "' . $directory . '"');
166 * Delete a remote file
167 * @param string $filename
170 public function deleteRemoteFile($filename)
172 $this->_secureConnection();
173 $this->_output('Deleting file "' . $filename . '"');
174 $result = ftp_delete($this->_connection, $filename);
178 error_log('Couldn\'t delete remote FTP file "' . $filename . '"');
186 * Download a remote file to a specified location.
187 * @param string $localFilename
188 * @param string $remoteFilename
190 * @return void|boolean
192 public function downloadFile($localFilename, $remoteFilename)
195 $this->_secureConnection();
196 $this->_output('Downloading file"' . $remoteFilename . '" to "' . $localFilename . '"');
197 ftp_pasv($this->_connection, true);
198 if(ftp_get($this->_connection, $localFilename, $remoteFilename, FTP_BINARY))
200 ftp_pasv($this->_connection, false);
203 ftp_pasv($this->_connection, false);
208 * Upload a specified file.
209 * @param string $localFilename
210 * @param string $remoteFilename
212 * @return void|boolean
214 public function uploadFile($localFilename, $remoteFilename, $mode = FTP_BINARY)
217 $this->_secureConnection();
218 $this->_output('Uploading file"' . $localFilename . '" to "' . $remoteFilename . '"');
219 ftp_pasv($this->_connection, true);
220 if(ftp_put($this->_connection, $remoteFilename, $localFilename, $mode))
222 ftp_pasv($this->_connection, false);
226 ftp_pasv($this->_connection, false);
228 $error = 'Couldn\'t upload "' . $localFilename . '" to remote location "' . $remoteFilename . '"';
230 throw new \Exception($error);
235 * Checks the connection and reconnects to the remote FTP server if necessary
237 protected function _secureConnection()
239 if(null == $this->_connection)
247 * Returns the present working directory
250 public function whereAmI()
252 $this->_secureConnection();
253 return (string) ftp_pwd($this->_connection);
256 protected function _output($string)
258 if(false == $this->_verbose)
262 echo $string . PHP_EOL;