phpseclib\Net\SFTP::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. $offset and $length can be used to download files in chunks.
public get ( string $remote_file, string $local_file = false, integer $offset, integer $length ) : mixed
$remote_file string
$local_file string
$offset integer
$length integer
return mixed
    function get($remote_file, $local_file = false, $offset = 0, $length = -1)
    {
        if (!($this->bitmap & SSH2::MASK_LOGIN)) {
            return false;
        }
        $remote_file = $this->_realpath($remote_file);
        if ($remote_file === false) {
            return false;
        }
        $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_READ, 0);
        if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
            return false;
        }
        $response = $this->_get_sftp_packet();
        switch ($this->packet_type) {
            case NET_SFTP_HANDLE:
                $handle = substr($response, 4);
                break;
            case NET_SFTP_STATUS:
                // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
                $this->_logError($response);
                return false;
            default:
                throw new \UnexpectedValueException('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
        }
        if (is_resource($local_file)) {
            $fp = $local_file;
            $stat = fstat($fp);
            $res_offset = $stat['size'];
        } else {
            $res_offset = 0;
            if ($local_file !== false) {
                $fp = fopen($local_file, 'wb');
                if (!$fp) {
                    return false;
                }
            } else {
                $content = '';
            }
        }
        $fclose_check = $local_file !== false && !is_resource($local_file);
        $start = $offset;
        $read = 0;
        while (true) {
            $i = 0;
            while ($i < NET_SFTP_QUEUE_SIZE && ($length < 0 || $read < $length)) {
                $tempoffset = $start + $read;
                $packet_size = $length > 0 ? min($this->max_sftp_packet, $length - $read) : $this->max_sftp_packet;
                $packet = pack('Na*N3', strlen($handle), $handle, $tempoffset / 4294967296, $tempoffset, $packet_size);
                if (!$this->_send_sftp_packet(NET_SFTP_READ, $packet)) {
                    if ($fclose_check) {
                        fclose($fp);
                    }
                    return false;
                }
                $packet = null;
                $read += $packet_size;
                $i++;
            }
            if (!$i) {
                break;
            }
            $clear_responses = false;
            while ($i > 0) {
                $i--;
                if ($clear_responses) {
                    $this->_get_sftp_packet();
                    continue;
                } else {
                    $response = $this->_get_sftp_packet();
                }
                switch ($this->packet_type) {
                    case NET_SFTP_DATA:
                        $temp = substr($response, 4);
                        $offset += strlen($temp);
                        if ($local_file === false) {
                            $content .= $temp;
                        } else {
                            fputs($fp, $temp);
                        }
                        $temp = null;
                        break;
                    case NET_SFTP_STATUS:
                        // could, in theory, return false if !strlen($content) but we'll hold off for the time being
                        $this->_logError($response);
                        $clear_responses = true;
                        // don't break out of the loop yet, so we can read the remaining responses
                        break;
                    default:
                        if ($fclose_check) {
                            fclose($fp);
                        }
                        throw new \UnexpectedValueException('Expected SSH_FXP_DATA or SSH_FXP_STATUS');
                }
                $response = null;
            }
            if ($clear_responses) {
                break;
            }
        }
        if ($length > 0 && $length <= $offset - $start) {
            if ($local_file === false) {
                $content = substr($content, 0, $length);
            } else {
                ftruncate($fp, $length + $res_offset);
            }
        }
        if ($fclose_check) {
            fclose($fp);
        }
        if (!$this->_close_handle($handle)) {
            return false;
        }
        // if $content isn't set that means a file was written to
        return isset($content) ? $content : true;
    }

Usage Example

Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function download($local, $remote)
 {
     $this->checkConnection();
     if (!$this->sftp->get($remote, $local)) {
         throw new \RuntimeException(implode($this->sftp->getSFTPErrors(), "\n"));
     }
 }
All Usage Examples Of phpseclib\Net\SFTP::get