public function put($local, $remote) { //make sure we are connected $this->connect(); $this->plugin->Logger('nutshell.plugin.transfer.scp')->info(sprintf('Uploading to remote file %s...', $remote)); if (is_resource($local)) { throw new TransferException(sprintf('SCP of file handle has not been implemented.', $remote)); } else { if (is_string($local)) { //upload using file name. $this->plugin->Logger('nutshell.plugin.transfer.scp')->debug('Uploading using local file path.'); //check for file existence if (!is_readable($local)) { throw new TransferException(sprintf('File %s not be found or not accessible', $local)); } //transfers the file if (!ssh2_scp_send($this->connection, $local, $remote, 0644)) { throw new TransferException(sprintf('Transfer of file %s to %s failed', $local, $remote)); } } else { throw new TransferException(sprintf('Invalid parameter %s: expected string or file handle', parse_r($local, true))); } } $this->plugin->Logger('nutshell.plugin.transfer.scp')->info(sprintf('File transfered to %s successfully.', $remote)); }
public function fetch($remote, $local) { //make sure we are connected $this->connect(); $this->plugin->Logger('nutshell.plugin.transfer.ftp')->info(sprintf('Fetching remote file %s...', $remote)); if (is_resource($local)) { //upload with file handle $this->plugin->Logger('nutshell.plugin.transfer.ftp')->debug('Fetching using file handle.'); //transfers the file in binary mode to avoid ASCII mode corruption if (!@ftp_fget($this->connection, $local, $remote, FTP_BINARY)) { throw new TransferException(sprintf('Transfer of %s to file handle failed', $remote)); } } else { if (is_string($local)) { //upload using file name. $this->plugin->Logger('nutshell.plugin.transfer.ftp')->debug('Fetching using local file path.'); //check for file existence $dir = dirname($local); if (is_file($local) && is_writable($local) || is_dir($dir) && is_writable($dir)) { //transfers the file in binary mode to avoid ASCII mode corruption if (!@ftp_get($this->connection, $local, $remote, FTP_BINARY)) { throw new TransferException(sprintf('Transfer of file %s to %s failed', $local, $remote)); } } else { throw new TransferException(sprintf('File %s not be found or not writable', $local)); } } else { throw new TransferException(sprintf('Invalid parameter %s: expected string or file handle', parse_r($local, true))); } } $this->plugin->Logger('nutshell.plugin.transfer.ftp')->info(sprintf('File %s fetched successfully.', $remote)); }
public function fetch($remote, $local) { //make sure we are connected $this->connect(); $this->plugin->Logger('nutshell.plugin.transfer.sftp')->debug(sprintf('Fetching remote file %s...', $remote)); //holder for the local file handle $flocal = null; $localClose = true; if (is_string($local)) { //upload using file name. $this->plugin->Logger('nutshell.plugin.transfer.sftp')->debug('Fetching using local file path.'); //check that the file is readable $dir = dirname($local); if (!(is_writable($local) || is_dir($dir) && is_writable($dir))) { throw new TransferException(sprintf('File %s cannot be created or is not writable', $local)); } //create the local handle if (!($flocal = fopen($local, 'wb'))) { throw new TransferException(sprintf('Failed to open local file %s in writing', $local)); } } else { if (is_resource($local)) { //upload with file handle $this->plugin->Logger('nutshell.plugin.transfer.sftp')->debug('Fetching using file handle.'); //nothing to do, just set the handle $flocal = $local; //don't close the handle if it was provided as an argument $localClose = false; } else { throw new TransferException(sprintf('Invalid parameter %s: expected string or file handle', parse_r($local, true))); } } //open the remote file descriptor $target = sprintf("ssh2.sftp://%s%s", $this->sftpHandle, $remote); if ($fremote = fopen($target, 'rb')) { $written = 0; //write chunks of data to the remote system while ($data = fread($fremote, self::TRANSFER_CHUNK)) { $written += fwrite($flocal, $data); } $this->plugin->Logger('nutshell.plugin.transfer.sftp')->debug(sprintf('Transfered %dB', $written)); //close the remote handle fclose($fremote); if ($localClose) { //close the local handle fclose($flocal); } } else { throw new TransferException(sprintf('Failed to open remote file %s in reading', $remote)); } $this->plugin->Logger('nutshell.plugin.transfer.sftp')->debug(sprintf('File transfered from %s successfully.', $remote)); }