FileTransferException example


  public static function factory($jail$settings) {
    throw new FileTransferException('FileTransfer::factory() static method not overridden by FileTransfer subclass.');
  }

  /** * Implements the magic __get() method. * * If the connection isn't set to anything, this will call the connect() * method and return the result; afterwards, the connection will be returned * directly without using this method. * * @param string $name * The name of the variable to return. * * @return string|bool * The variable specified in $name. */

class FTPExtension extends FTP implements ChmodInterface {

  /** * {@inheritdoc} */
  public function connect() {
    $this->connection = ftp_connect($this->hostname, $this->port);

    if (!$this->connection) {
      throw new FileTransferException("Cannot connect to FTP Server, check settings");
    }
    if (!ftp_login($this->connection, $this->username, $this->password)) {
      throw new FileTransferException("Cannot log in to FTP server. Check username and password");
    }
  }

  /** * {@inheritdoc} */
  protected function copyFileJailed($source$destination) {
    if (!@ftp_put($this->connection, $destination$source, FTP_BINARY)) {
      

  public static function factory($jail$settings) {
    return new Local($jail, \Drupal::service('file_system'));
  }

  /** * {@inheritdoc} */
  protected function copyFileJailed($source$destination) {
    if (@!copy($source$destination)) {
      throw new FileTransferException('Cannot copy %source to %destination.', 0, ['%source' => $source, '%destination' => $destination]);
    }
  }

  /** * {@inheritdoc} */
  protected function createDirectoryJailed($directory) {
    if (!is_dir($directory) && @!mkdir($directory, 0777, TRUE)) {
      throw new FileTransferException('Cannot create directory %directory.', 0, ['%directory' => $directory]);
    }
  }

  

  public static function factory($jail$settings) {
    $username = empty($settings['username']) ? '' : $settings['username'];
    $password = empty($settings['password']) ? '' : $settings['password'];
    $hostname = empty($settings['advanced']['hostname']) ? 'localhost' : $settings['advanced']['hostname'];
    $port = empty($settings['advanced']['port']) ? 21 : $settings['advanced']['port'];

    if (function_exists('ftp_connect')) {
      $class = 'Drupal\Core\FileTransfer\FTPExtension';
    }
    else {
      throw new FileTransferException('No FTP backend available.');
    }

    return new $class($jail$username$password$hostname$port);
  }

  /** * {@inheritdoc} */
  public function getSettingsForm() {
    $form = parent::getSettingsForm();
    $form['advanced']['port']['#default_value'] = 21;
    
$this->hostname = $hostname;
    $this->port = $port;
    parent::__construct($jail);
  }

  /** * {@inheritdoc} */
  public function connect() {
    $this->connection = @ssh2_connect($this->hostname, $this->port);
    if (!$this->connection) {
      throw new FileTransferException('SSH Connection failed to @host:@port', 0, ['@host' => $this->hostname, '@port' => $this->port]);
    }
    if (!@ssh2_auth_password($this->connection, $this->username, $this->password)) {
      throw new FileTransferException('The supplied username/password combination was not accepted.');
    }
  }

  /** * {@inheritdoc} */
  public static function factory($jail$settings) {
    $username = empty($settings['username']) ? '' : $settings['username'];
    
Home | Imprint | This part of the site doesn't use cookies.