phpseclib\Net\SCP::put PHP Method

put() public method

By default, \phpseclib\Net\SCP::put() does not read from the local filesystem. $data is dumped directly into $remote_file. So, for example, if you set $data to 'filename.ext' and then do \phpseclib\Net\SCP::get(), you will get a file, twelve bytes long, containing 'filename.ext' as its contents. Setting $mode to self::SOURCE_LOCAL_FILE will change the above behavior. With self::SOURCE_LOCAL_FILE, $remote_file will contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how large $remote_file will be, as well. Currently, only binary mode is supported. As such, if the line endings need to be adjusted, you will need to take care of that, yourself.
public put ( string $remote_file, string $data, integer $mode = self::SOURCE_STRING, callable $callback = null ) : boolean
$remote_file string
$data string
$mode integer
$callback callable
return boolean
    function put($remote_file, $data, $mode = self::SOURCE_STRING, $callback = null)
    {
        if (!isset($this->ssh)) {
            return false;
        }
        if (!$this->ssh->exec('scp -t ' . escapeshellarg($remote_file), false)) {
            // -t = to
            return false;
        }
        $temp = $this->_receive();
        if ($temp !== chr(0)) {
            return false;
        }
        if ($this->mode == self::MODE_SSH2) {
            $this->packet_size = $this->ssh->packet_size_client_to_server[SSH2::CHANNEL_EXEC] - 4;
        }
        $remote_file = basename($remote_file);
        if ($mode == self::SOURCE_STRING) {
            $size = strlen($data);
        } else {
            if (!is_file($data)) {
                throw new FileNotFoundException("{$data} is not a valid file");
            }
            $fp = @fopen($data, 'rb');
            if (!$fp) {
                return false;
            }
            $size = filesize($data);
        }
        $this->_send('C0644 ' . $size . ' ' . $remote_file . "\n");
        $temp = $this->_receive();
        if ($temp !== chr(0)) {
            return false;
        }
        $sent = 0;
        while ($sent < $size) {
            $temp = $mode & self::SOURCE_STRING ? substr($data, $sent, $this->packet_size) : fread($fp, $this->packet_size);
            $this->_send($temp);
            $sent += strlen($temp);
            if (is_callable($callback)) {
                call_user_func($callback, $sent);
            }
        }
        $this->_close();
        if ($mode != self::SOURCE_STRING) {
            fclose($fp);
        }
        return true;
    }

Usage Example

 /**
  * Create backup user over ssh
  * @param string $addr
  * @param string $port
  * @return mixed (false or identity of the router)
  */
 public function createBackupAccount($addr, $port)
 {
     $bcpuser = $this->config['routerboard']['backupuser'];
     $keyname = 'id_rsa-backup-user.pub';
     if ($ssh = $this->sshConnect($addr, $port, false)) {
         $scp = new SCP($ssh);
         if (!$scp->put($keyname, $this->config['system']['ssh-dir'] . DIRECTORY_SEPARATOR . 'id_rsa.pub', SCP::SOURCE_LOCAL_FILE)) {
             $this->logger->log("The SSH-RSA file copy to the :'" . $addr . "' router fails!", $this->logger->setError());
             return false;
         }
         $ssh->exec('user add name=' . $bcpuser . ' group=full');
         sleep(1);
         $ssh->exec('user ssh-keys import user='******' public-key-file=' . $keyname);
         sleep(1);
         if ($ssh->exec('user comment ' . $bcpuser . ' comment="Backup User"')) {
             $this->logger->log("Creating of the backup account '" . $bcpuser . "' fails!", $this->logger->setError());
             return false;
         }
         sleep(1);
         $identity = $ssh->exec('system identity print');
         $identity = trim(str_replace('name:', '', trim($identity)));
         $this->logger->log("The backup account '" . $bcpuser . "' at '" . $identity . "'@'" . $addr . "' has been created successfully!");
         $this->sshDisconnect($ssh);
         return $identity;
     }
     return false;
 }
All Usage Examples Of phpseclib\Net\SCP::put