_serverAddress = $serverAddress; $this->_username = $username; $this->_password = $password; $this->_verbose = $verbose; $this->_timeout = $timeout; } /** * Change remote FTP directory. * @param unknown $directory * @throws \ErrorException * @return boolean */ public function changeDirectory($directory) { $this->_secureConnection(); $this->_output('Changing directory to "' . $directory . '"'); $result = ftp_chdir($this->_connection, $directory); if(false == $result) { error_log('Couldn\'t change to FTP directory "' . $directory . '"'); throw new \Exception('Couldn\'t change to FTP directory "' . $directory . '"'); return false; } return $result; } /** * Create a new remote directory. * @param string $directory * @throws \Exception * @return boolean */ public function createDirectory($directory) { $this->_secureConnection(); $this->_output('Creating directory "' . $directory . '"'); $result = ftp_mkdir($this->_connection, $directory); if(false == $result) { $error = 'Couldn\'t create remote FTP directory "' . $directory . '"' ; error_log($error); throw new \Exception($error); return false; } return true; } /** * Connect to the remote server. * @throws \Exception * @return \Utility\Remote\Ftp */ public function connect() { $this->_connection = ftp_connect($this->_serverAddress); if(false == $this->_connection) { throw new \Exception("Couldn't connect to FTP server: " . $this->_serverAddress ); } $this->_output('Connected to "' . $this->_serverAddress . '"'); return $this; } /** * Close the remote FTP server connection. */ public function closeConnection() { $this->_output('Closing FTP connection'); ftp_close($this->_connection); $this->_connection = null; } /** * Login to the remote FTP server. * @throws \Exception * @return \Utility\Remote\Ftp */ protected function _login() { $this->_output('Logging in to "' . $this->_serverAddress . '"'); $logingResult = ftp_login($this->_connection, $this->_username, $this->_password); if(false == $logingResult) { $this->_output('Login failure'); $this->closeConnection(); throw new \Exception("Couldn't log into remote server, please check your credentials and try again"); } $this->_output('Logged in to "' . $this->_serverAddress . '"'); return $this; } /** * $return the content of a directory. * Returns an empty array on error. * @param string $directory * @return array */ public function getDirectoryContents($directory = '.') { $this->_secureConnection(); $this->_output('Retrieving direcory contents for "' . $directory . '"'); $contents = ftp_nlist($this->_connection, '.'); if(false == $contents) { error_log('Couldn\'t retrieve directory contents for: ' . $this->_serverAddress . ', directory: "' . $directory . '"'); return array(); } return $contents; } /** * Delete a remote Directory * @example deleteRemoteDirectory('./relative-path-directory'); * @example deleteRemoteDirectory('/temp/absolute-path-directory'); * @param string $directory * @return boolean */ public function deleteRemoteDirectory($directory) { $this->_secureConnection(); $this->_output('Deleting direcory "' . $directory . '"'); $result = ftp_rmdir($this->_connection, $directory); if(false == $result) { error_log('Couldn\'t delete remote FTP directory "' . $directory . '"'); return false; } return true; } /** * Delete a remote file * @param string $filename * @return boolean */ public function deleteRemoteFile($filename) { $this->_secureConnection(); $this->_output('Deleting file "' . $filename . '"'); $result = ftp_delete($this->_connection, $filename); if(false == $result) { error_log('Couldn\'t delete remote FTP file "' . $filename . '"'); return false; } return true; } /** * Download a remote file to a specified location. * @param string $localFilename * @param string $remoteFilename * @throws \Exception * @return void|boolean */ public function downloadFile($localFilename, $remoteFilename) { set_time_limit(0); $this->_secureConnection(); $this->_output('Downloading file"' . $remoteFilename . '" to "' . $localFilename . '"'); ftp_pasv($this->_connection, true); if(ftp_get($this->_connection, $localFilename, $remoteFilename, FTP_BINARY)) { ftp_pasv($this->_connection, false); return true; } ftp_pasv($this->_connection, false); return false; } /** * Upload a specified file. * @param string $localFilename * @param string $remoteFilename * @throws \Exception * @return void|boolean */ public function uploadFile($localFilename, $remoteFilename, $mode = FTP_BINARY) { set_time_limit(0); $this->_secureConnection(); $this->_output('Uploading file"' . $localFilename . '" to "' . $remoteFilename . '"'); ftp_pasv($this->_connection, true); if(ftp_put($this->_connection, $remoteFilename, $localFilename, $mode)) { ftp_pasv($this->_connection, false); return true; } ftp_pasv($this->_connection, false); $error = 'Couldn\'t upload "' . $localFilename . '" to remote location "' . $remoteFilename . '"'; error_log($error); throw new \Exception($error); return false; } /** * Checks the connection and reconnects to the remote FTP server if necessary */ protected function _secureConnection() { if(null == $this->_connection) { $this->connect(); $this->_login(); } } /** * Returns the present working directory * @return string */ public function whereAmI() { $this->_secureConnection(); return (string) ftp_pwd($this->_connection); } protected function _output($string) { if(false == $this->_verbose) { return; } echo $string . PHP_EOL; } }