public function tearDown() { if ($this->sftp) { $this->sftp->chdir($this->getEnv('SSH_HOME')); $this->sftp->delete($this->scratchDir); } parent::tearDown(); }
/** * Delete directory and all its contents. * * @param string $absolutePath The absolute path to directory * @param bool $recursive The option to delete recursively * * @return SftpInterface The current instance * * @api */ public function deleteDirectory(string $absolutePath, bool $recursive = false) : SftpInterface { /* Requires absolute PATH. */ $this->netSftp->chdir(dirname($absolutePath)); $this->netSftp->delete(basename($absolutePath), $this->toBoolean($recursive)); return $this; }
/** * {@inheritdoc} */ public function changeWorkingDirectory($remoteDirectory) { if ($this->isConnected()) { $this->executeCommand('cd', array($remoteDirectory)); return $this->connection->chdir($remoteDirectory); } return false; }
/** * @param string $Name * * @return SFTP * @throws ComponentException */ public function changeDirectory($Name) { $this->persistConnection(); if (!$this->Connection->chdir($Name)) { throw new ComponentException(__METHOD__ . ': Failed'); } return $this; }
public function exists($path) { if (false === $this->isConnected()) { $this->connectAndLogin(); } $pwd = $this->sftp->pwd(); // try to change directory to see if it is an existing directory $result = $this->sftp->chdir($path); if (true === $result) { $result = $this->sftp->chdir($pwd); // change back to the original directory return true; } else { // list the parent directory and check if the file exists $parent = dirname($path); $result = $this->sftp->nlist($parent); if (false !== $result) { if (in_array(basename($path), $result)) { return true; } } } return false; }
/** * (non-PHPDoc) * * @see \phpbu\App\Backup\Sync::sync() * @param \phpbu\App\Backup\Target $target * @param \phpbu\App\Result $result * @throws \phpbu\App\Backup\Sync\Exception */ public function sync(Target $target, Result $result) { // silence phpseclib $old = error_reporting(0); $sftp = new phpseclib\Net\SFTP($this->host); if (!$sftp->login($this->user, $this->password)) { error_reporting($old); throw new Exception(sprintf('authentication failed for %s@%s%s', $this->user, $this->host, empty($this->password) ? '' : ' with password ****')); } error_reporting($old); $remoteFilename = $target->getFilename(); $localFile = $target->getPathname(); if ('' !== $this->remotePath) { $remoteDirs = explode('/', $this->remotePath); foreach ($remoteDirs as $dir) { if (!$sftp->is_dir($dir)) { $result->debug(sprintf('creating remote dir \'%s\'', $dir)); $sftp->mkdir($dir); } $result->debug(sprintf('change to remote dir \'%s\'', $dir)); $sftp->chdir($dir); } } $result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename)); $result->debug(sprintf('last error \'%s\'', $sftp->getLastSFTPError())); /** @noinspection PhpInternalEntityUsedInspection */ if (!$sftp->put($remoteFilename, $localFile, phpseclib\Net\SFTP::SOURCE_LOCAL_FILE)) { throw new Exception(sprintf('error uploading file: %s - %s', $localFile, $sftp->getLastSFTPError())); } }