phpseclib\Net\SCP::get PHP Method

get() public method

Returns a string containing the contents of $remote_file if $local_file is left undefined or a boolean false if the operation was unsuccessful. If $local_file is defined, returns true or false depending on the success of the operation
public get ( string $remote_file, string $local_file = false ) : mixed
$remote_file string
$local_file string
return mixed
    function get($remote_file, $local_file = false)
    {
        if (!isset($this->ssh)) {
            return false;
        }
        if (!$this->ssh->exec('scp -f ' . escapeshellarg($remote_file), false)) {
            // -f = from
            return false;
        }
        $this->_send("");
        if (!preg_match('#(?<perms>[^ ]+) (?<size>\\d+) (?<name>.+)#', rtrim($this->_receive()), $info)) {
            return false;
        }
        $this->_send("");
        $size = 0;
        if ($local_file !== false) {
            $fp = @fopen($local_file, 'wb');
            if (!$fp) {
                return false;
            }
        }
        $content = '';
        while ($size < $info['size']) {
            $data = $this->_receive();
            // SCP usually seems to split stuff out into 16k chunks
            $size += strlen($data);
            if ($local_file === false) {
                $content .= $data;
            } else {
                fputs($fp, $data);
            }
        }
        $this->_close();
        if ($local_file !== false) {
            fclose($fp);
            return true;
        }
        return $content;
    }

Usage Example

 /**
  * @depends testPutGetString
  * @param \phpseclib\Net\SCP $scp
  */
 public function testGetFile($scp)
 {
     $localFilename = $this->createTempFile();
     $this->assertTrue($scp->get(self::$remoteFile, $localFilename), 'Failed asserting that get() into file was successful.');
     // TODO: Address https://github.com/phpseclib/phpseclib/issues/146
     $this->assertContains(filesize($localFilename), array(self::$exampleDataLength, self::$exampleDataLength + 1), 'Failed asserting that filesize matches expected data size.');
     $this->assertContains(file_get_contents($localFilename), array(self::$exampleData, self::$exampleData . ""), 'Failed asserting that file content matches expected content.');
 }
All Usage Examples Of phpseclib\Net\SCP::get