phpseclib API Documentation
Class

Net_SFTP

class Net_SFTP extends Net_SSH2

Pure-PHP implementations of SFTP.

Properties

string $identifier The SSH identifier
object $fsock The Socket Object
int $bitmap Execution Bitmap
string $errors Error information
array|false $server_identifier Server Identifier
array|false $kex_algorithms Key Exchange Algorithms
string|false $kex_algorithm Key Exchange Algorithm
int $kex_dh_group_size_min Minimum Diffie-Hellman Group Bit Size in RFC 4419 Key Exchange Methods
int $kex_dh_group_size_preferred Preferred Diffie-Hellman Group Bit Size in RFC 4419 Key Exchange Methods
int $kex_dh_group_size_max Maximum Diffie-Hellman Group Bit Size in RFC 4419 Key Exchange Methods
array|false $server_host_key_algorithms Server Host Key Algorithms
array|false $encryption_algorithms_client_to_server Encryption Algorithms: Client to Server
array|false $encryption_algorithms_server_to_client Encryption Algorithms: Server to Client
array|false $mac_algorithms_client_to_server MAC Algorithms: Client to Server
array|false $mac_algorithms_server_to_client MAC Algorithms: Server to Client
array|false $compression_algorithms_client_to_server Compression Algorithms: Client to Server
array|false $compression_algorithms_server_to_client Compression Algorithms: Server to Client
array|false $languages_server_to_client Languages: Server to Client
array|false $languages_client_to_server Languages: Client to Server
array $preferred Preferred Algorithms
int $encrypt_block_size Block Size for Server to Client Encryption
int $decrypt_block_size Block Size for Client to Server Encryption
object $decrypt Server to Client Encryption Object
object $encrypt Client to Server Encryption Object
object $hmac_create Client to Server HMAC Object
object $hmac_check Server to Client HMAC Object
int $hmac_size Size of server to client HMAC
string $server_public_host_key Server Public Host Key
string $session_id Session identifier
string $exchange_hash Exchange hash
array $message_numbers Message Numbers
array $disconnect_reasons Disconnection Message 'reason codes' defined in RFC4253
array $channel_open_failure_reasons SSHMSGCHANNELOPENFAILURE 'reason codes', defined in RFC4254
array $terminal_modes Terminal Modes
array $channel_extended_data_type_codes SSHMSGCHANNELEXTENDEDDATA's datatypecodes
int $send_seq_no Send Sequence Number
int $get_seq_no Get Sequence Number
array $server_channels Server Channels
array $channel_buffers Channel Buffers
array $channel_status Channel Status
array $packet_size_client_to_server Packet Size
array $message_number_log Message Number Log
array $message_log Message Log
int $window_size The Window Size
array $window_size_server_to_client Window size, server to client
array $window_size_client_to_server Window size, client to server
string $signature Server signature
string $signature_format Server signature format
array $interactiveBuffer Interactive Buffer
int $log_size Current log size
$timeout Timeout
$curTimeout Current Timeout
resource $realtime_log_file Real-time log file pointer
int $realtime_log_size Real-time log file size
bool $signature_validated Has the signature been validated?
$realtime_log_wrap Real-time log file wrap boolean
$quiet_mode Flag to suppress stderr from output
int $last_packet Time of first network activity
int $exit_status Exit status returned from ssh if any
bool $request_pty Flag to request a PTY when using exec()
bool $in_request_pty_exec Flag set while exec() is running when using enablePTY()
bool $in_subsystem Flag set after startSubsystem() is called
string $stdErrorLog Contents of stdError
string $last_interactive_response The Last Interactive Response
array $keyboard_requests_responses Keyboard Interactive Request / Responses
string $banner_message Banner Message
bool $is_timeout Did read() timeout or return normally?
string $log_boundary Log Boundary
int $log_long_width Log Long Width
int $log_short_width Log Short Width
string $host Hostname
int $port Port Number
int $windowColumns Number of columns for terminal window size
int $windowRows Number of columns for terminal window size
int $crypto_engine Crypto Engine
System_SSH_Agent $agent A SystemSSHAgent for use in the SSH2 Agent Forwarding scenario
bool $send_id_string_first Send the identification string first?
bool $send_kex_first Send the key exchange initiation packet first?
bool $bad_key_size_fix Some versions of OpenSSH incorrectly calculate the key size
bool $retry_connect Should we try to re-connect to re-establish keys?
string|false $binary_packet_buffer Binary Packet Buffer
string|false $preferred_signature_format Preferred Signature Format
array $auth Authentication Credentials
array $packet_types Packet Types
array $status_codes Status Codes
boolean $use_request_id The Request ID
int $packet_type The Packet Type
string $packet_buffer Packet Buffer
array $extensions Extensions supported by the server
int $version Server SFTP version
string $pwd Current working directory
array $packet_type_log Packet Type Log
array $packet_log Packet Log
array $sftp_errors Error information
array $stat_cache Stat Cache
array $max_sftp_packet Max SFTP Packet Size
bool $use_stat_cache Stat Cache Flag
array $sortOptions Sort Options
bool $canonicalize_paths Canonicalization Flag
array $requestBuffer Request Buffers

Methods

Net_SFTP __construct(string $host, int $port = 22, int $timeout = 10)

Default Constructor.

Net_SSH2(mixed $host, int $port = 22, int $timeout = 10)

PHP4 compatible Default Constructor.

from Net_SSH2
setCryptoEngine(int $engine)

Set Crypto Engine Mode

from Net_SSH2
sendIdentificationStringFirst()

Send Identification String First

from Net_SSH2
sendIdentificationStringLast()

Send Identification String Last

from Net_SSH2
sendKEXINITFirst()

Send SSHMSGKEXINIT First

from Net_SSH2
sendKEXINITLast()

Send SSHMSGKEXINIT Last

from Net_SSH2
bool _connect()

Connect to an SSHv2 server

from Net_SSH2
string _generate_identifier()

Generates the SSH identifier

from Net_SSH2
_key_exchange(string $kexinit_payload_server = false)

Key Exchange

from Net_SSH2
int|null _encryption_algorithm_to_key_size(string $algorithm)

Maps an encryption algorithm name to the number of key bytes.

from Net_SSH2
mixed _encryption_algorithm_to_crypt_instance(string $algorithm)

Maps an encryption algorithm name to an instance of a subclass of \phpseclib\Crypt\Base.

from Net_SSH2
bool _bad_algorithm_candidate(string $algorithm)

Tests whether or not proposed algorithm has a potential for issues

from Net_SSH2
bool login($username)

Login

bool _login($username)

Login Helper

from Net_SSH2
bool _login_helper(string $username, string $password = null)

Login Helper

from Net_SSH2
bool _keyboard_interactive_login(string $username, string $password)

Login via keyboard-interactive authentication

from Net_SSH2
bool _keyboard_interactive_process()

Handle the keyboard-interactive requests / responses.

from Net_SSH2
bool _ssh_agent_login(string $username, System_SSH_Agent $agent)

Login with an ssh-agent provided key

from Net_SSH2
bool _privatekey_login($username, $privatekey)

Login with an RSA private key

from Net_SSH2
setTimeout(mixed $timeout)

Set Timeout

from Net_SSH2
getStdError()

Get the output from stdError

from Net_SSH2
string exec(string $command, Callback $callback = null)

Execute Command

from Net_SSH2
bool _initShell()

Creates an interactive shell

from Net_SSH2
int _get_interactive_channel()

Return the channel to be used with read() / write()

from Net_SSH2
int _get_open_channel()

Return an available open channel

from Net_SSH2
string read(string $expect = '', int $mode = NET_SSH2_READ_SIMPLE)

Returns the output of an interactive shell

from Net_SSH2
bool write(string $cmd)

Inputs a command into an interactive shell.

from Net_SSH2
bool startSubsystem(string $subsystem)

Start a subsystem.

from Net_SSH2
bool stopSubsystem()

Stops a subsystem.

from Net_SSH2
reset()

Closes a channel

from Net_SSH2
isTimeout()

Is timeout?

from Net_SSH2
disconnect()

Disconnect

from Net_SSH2
__destruct()

Destructor.

from Net_SSH2
bool isConnected()

Is the connection still active?

from Net_SSH2
bool isAuthenticated()

Have you successfully been logged in?

from Net_SSH2
bool ping()

Pings a server connection, or tries to reconnect if the connection has gone down

from Net_SSH2
boolean _reconnect()

In situ reconnect method

from Net_SSH2
_reset_connection(int $reason)

Resets a connection for re-use

string _get_binary_packet($skip_channel_filter = false)

Gets Binary Packets

from Net_SSH2
string _filter($payload, $skip_channel_filter)

Filter Binary Packets

from Net_SSH2
enableQuietMode()

Enable Quiet Mode

from Net_SSH2
disableQuietMode()

Disable Quiet Mode

from Net_SSH2
bool isQuietModeEnabled()

Returns whether Quiet Mode is enabled or not

from Net_SSH2
enablePTY()

Enable request-pty when using exec()

from Net_SSH2
disablePTY()

Disable request-pty when using exec()

from Net_SSH2
bool isPTYEnabled()

Returns whether request-pty is enabled or not

from Net_SSH2
mixed _get_channel_packet($client_channel, $skip_extended = false)

Gets channel data

from Net_SSH2
bool _send_binary_packet(string $data, string $logged = null)

Sends Binary Packets

from Net_SSH2
_append_log($message_number, $message)

Logs data packets

from Net_SSH2
bool _send_channel_packet(int $client_channel, string $data)

Sends channel data

from Net_SSH2
bool _close_channel(int $client_channel, bool $want_reply = false)

Closes and flushes a channel

from Net_SSH2
bool _disconnect(int $reason)

Disconnect

string _string_shift(string $string, int $index = 1)

String Shift

from Net_SSH2
_define_array()

Define Array

from Net_SSH2
array|false|string getLog()

Returns a log of the packets that have been sent and received.

from Net_SSH2
string _format_log(array $message_log, array $message_number_log)

Formats a log for printing

from Net_SSH2
string _format_log_helper(array $matches)

Helper function for formatlog

from Net_SSH2
_on_channel_open()

Helper function for agent->onchannel_open()

from Net_SSH2
mixed _array_intersect_first(array $array1, array $array2)

Returns the first value of the intersection of two arrays or false if the intersection is empty.

from Net_SSH2
string[] getErrors()

Returns all errors

from Net_SSH2
string getLastError()

Returns the last error

from Net_SSH2
string getServerIdentification()

Return the server identification.

from Net_SSH2
array getKexAlgorithms()

Return a list of the key exchange algorithms the server supports.

from Net_SSH2
array getServerHostKeyAlgorithms()

Return a list of the host key (public key) algorithms the server supports.

from Net_SSH2
array getEncryptionAlgorithmsClient2Server()

Return a list of the (symmetric key) encryption algorithms the server supports, when receiving stuff from the client.

from Net_SSH2
array getEncryptionAlgorithmsServer2Client()

Return a list of the (symmetric key) encryption algorithms the server supports, when sending stuff to the client.

from Net_SSH2
array getMACAlgorithmsClient2Server()

Return a list of the MAC algorithms the server supports, when receiving stuff from the client.

from Net_SSH2
array getMACAlgorithmsServer2Client()

Return a list of the MAC algorithms the server supports, when sending stuff to the client.

from Net_SSH2
array getCompressionAlgorithmsClient2Server()

Return a list of the compression algorithms the server supports, when receiving stuff from the client.

from Net_SSH2
array getCompressionAlgorithmsServer2Client()

Return a list of the compression algorithms the server supports, when sending stuff to the client.

from Net_SSH2
array getLanguagesServer2Client()

Return a list of the languages the server supports, when sending stuff to the client.

from Net_SSH2
array getLanguagesClient2Server()

Return a list of the languages the server supports, when receiving stuff from the client.

from Net_SSH2
array getServerAlgorithms()

Returns a list of algorithms the server supports

from Net_SSH2
array getSupportedKEXAlgorithms()

Returns a list of KEX algorithms that phpseclib supports

from Net_SSH2
array getSupportedHostKeyAlgorithms()

Returns a list of host key algorithms that phpseclib supports

from Net_SSH2
array getSupportedEncryptionAlgorithms()

Returns a list of symmetric key algorithms that phpseclib supports

from Net_SSH2
array getSupportedMACAlgorithms()

Returns a list of MAC algorithms that phpseclib supports

from Net_SSH2
array getSupportedCompressionAlgorithms()

Returns a list of compression algorithms that phpseclib supports

from Net_SSH2
array getAlgorithmsNegotiated()

Return list of negotiated algorithms

from Net_SSH2
setPreferredAlgorithms(array $methods)

Accepts an associative array with up to four parameters as described at https://www.php.net/manual/en/function.ssh2-connect.php

from Net_SSH2
string getBannerMessage()

Returns the banner message.

from Net_SSH2
mixed getServerPublicHostKey()

Returns the server public host key.

from Net_SSH2
false|int getExitStatus()

Returns the exit status of an SSH command or false.

from Net_SSH2
int getWindowColumns()

Returns the number of columns for the terminal window size.

from Net_SSH2
int getWindowRows()

Returns the number of rows for the terminal window size.

from Net_SSH2
setWindowColumns(int $value)

Sets the number of columns for the terminal window size.

from Net_SSH2
setWindowRows(int $value)

Sets the number of rows for the terminal window size.

from Net_SSH2
setWindowSize(int $columns = 80, int $rows = 24)

Sets the number of columns and rows for the terminal window size.

from Net_SSH2
Net_SFTP(string $host, int $port = 22, int $timeout = 10)

PHP4 compatible Default Constructor.

disableStatCache()

Disable the stat cache

enableStatCache()

Enable the stat cache

clearStatCache()

Clear the stat cache

enablePathCanonicalization()

Enable path canonicalization

disablePathCanonicalization()

Enable path canonicalization

mixed pwd()

Returns the current directory name

_logError(string $response, int $status = -1)

Logs errors

mixed realpath(string $path)

Returns canonicalized absolute pathname

mixed _realpath(string $path)

Canonicalize the Server-Side Path Name

bool chdir(string $dir)

Changes the current directory

mixed nlist(string $dir = '.', bool $recursive = false)

Returns a list of files in the given directory

mixed _nlist_helper(string $dir, bool $recursive, string $relativeDir)

Helper method for nlist

mixed rawlist(string $dir = '.', bool $recursive = false)

Returns a detailed list of files in the given directory

mixed _list(string $dir, bool $raw = true)

Reads a list, be it detailed or not, of files in the given directory

int _comparator(array $a, array $b)

Compares two rawlist entries using parameters set by setListOrder()

setListOrder()

Defines how nlist() and rawlist() will be sorted - if at all.

mixed size(string $filename)

Returns the file size, in bytes, or false, on failure

_update_stat_cache(string $path, mixed $value)

Save files / directories to cache

bool _remove_from_stat_cache(string $path)

Remove files / directories from cache

mixed _query_stat_cache($path)

Checks cache for path

mixed stat(string $filename)

Returns general information about a file.

mixed lstat(string $filename)

Returns general information about a file or symbolic link.

mixed _stat(string $filename, int $type)

Returns general information about a file or symbolic link

bool truncate(string $filename, int $new_size)

Truncates a file to a given length

bool touch(string $filename, int $time = null, int $atime = null)

Sets access and modification time of file.

bool chown(string $filename, int $uid, bool $recursive = false)

Changes file or directory owner

bool chgrp(string $filename, int $gid, bool $recursive = false)

Changes file or directory group

mixed chmod(int $mode, string $filename, bool $recursive = false)

Set permissions on a file.

bool _setstat(string $filename, string $attr, bool $recursive)

Sets information about a file

bool _setstat_recursive(string $path, string $attr, int $i)

Recursively sets information on directories on the SFTP server

mixed readlink(string $link)

Return the target of a symbolic link

bool symlink(string $target, string $link)

Create a symlink

bool mkdir($dir, $mode = -1, $recursive = false)

Creates a directory.

bool _mkdir_helper($dir, $attr)

Helper function for directory creation

bool rmdir(string $dir)

Removes a directory.

bool put(string $remote_file, string|resource $data, int $mode = NET_SFTP_STRING, int $start = -1, int $local_start = -1, callable|null $progressCallback = null)

Uploads a file to the SFTP server.

bool _read_put_responses(int $i)

Reads multiple successive SSHFXPWRITE responses

bool _close_handle(string $handle)

Close handle

mixed get(string $remote_file, string $local_file = false, int $offset, int $length = -1, callable|null $progressCallback = null)

Downloads a file from the SFTP server.

bool delete(string $path, bool $recursive = true)

Deletes a file on the SFTP server.

bool _delete_recursive(string $path, int $i)

Recursively deletes directories on the SFTP server

bool file_exists(string $path)

Checks whether a file or directory exists

bool is_dir(string $path)

Tells whether the filename is a directory

bool is_file(string $path)

Tells whether the filename is a regular file

bool is_link(string $path)

Tells whether the filename is a symbolic link

bool is_readable(string $path)

Tells whether a file exists and is readable

bool is_writable(string $path)

Tells whether the filename is writable

bool is_writeable(string $path)

Tells whether the filename is writeable

mixed fileatime(string $path)

Gets last access time of file

mixed filemtime(string $path)

Gets file modification time

mixed fileperms(string $path)

Gets file permissions

mixed fileowner(string $path)

Gets file owner

mixed filegroup(string $path)

Gets file group

mixed filesize(string $path)

Gets file size

mixed filetype(string $path)

Gets file type

mixed _get_stat_cache_prop(string $path, string $prop)

Return a stat properity

mixed _get_lstat_cache_prop(string $path, string $prop)

Return an lstat properity

mixed _get_xstat_cache_prop($path, $prop, $type)

Return a stat or lstat properity

bool rename(string $oldname, string $newname)

Renames a file or a directory on the SFTP server

array _parseAttributes(string $response)

Parse Attributes

int _parseMode(int $mode)

Attempt to identify the file type

mixed _parseLongname(string $longname)

Parse Longname

bool _send_sftp_packet($type, $data, $request_id = 1)

Sends SFTP Packets

string _get_sftp_packet($request_id = null)

Receives SFTP Packets

string getSFTPLog()

Returns a log of the packets that have been sent and received.

array getSFTPErrors()

Returns all errors

string getLastSFTPError()

Returns the last error

array getSupportedVersions()

Get supported SFTP versions

Details

at line 314
public Net_SFTP __construct(string $host, int $port = 22, int $timeout = 10)

Default Constructor.

Connects to an SFTP server

Parameters

string $host
int $port
int $timeout

Return Value

Net_SFTP

in Net_SSH2 at line 1086
public Net_SSH2(mixed $host, int $port = 22, int $timeout = 10)

PHP4 compatible Default Constructor.

Parameters

mixed $host
int $port
int $timeout

See also

self::__construct()

in Net_SSH2 at line 1100
public setCryptoEngine(int $engine)

Set Crypto Engine Mode

Possible $engine values: CRYPTMODEINTERNAL, CRYPTMODEMCRYPT

Parameters

int $engine

in Net_SSH2 at line 1114
public sendIdentificationStringFirst()

Send Identification String First

https://tools.ietf.org/html/rfc4253#section-4.2 says "when the connection has been established, both sides MUST send an identification string". It does not say which side sends it first. In theory it shouldn't matter but it is a fact of life that some SSH servers are simply buggy

in Net_SSH2 at line 1128
public sendIdentificationStringLast()

Send Identification String Last

https://tools.ietf.org/html/rfc4253#section-4.2 says "when the connection has been established, both sides MUST send an identification string". It does not say which side sends it first. In theory it shouldn't matter but it is a fact of life that some SSH servers are simply buggy

in Net_SSH2 at line 1142
public sendKEXINITFirst()

Send SSHMSGKEXINIT First

https://tools.ietf.org/html/rfc4253#section-7.1 says "key exchange begins by each sending sending the [SSHMSGKEXINIT] packet". It does not say which side sends it first. In theory it shouldn't matter but it is a fact of life that some SSH servers are simply buggy

in Net_SSH2 at line 1156
public sendKEXINITLast()

Send SSHMSGKEXINIT Last

https://tools.ietf.org/html/rfc4253#section-7.1 says "key exchange begins by each sending sending the [SSHMSGKEXINIT] packet". It does not say which side sends it first. In theory it shouldn't matter but it is a fact of life that some SSH servers are simply buggy

in Net_SSH2 at line 1167
public bool _connect()

Connect to an SSHv2 server

Return Value

bool

in Net_SSH2 at line 1304
public string _generate_identifier()

Generates the SSH identifier

You should overwrite this method in your own class if you want to use another identifier

Return Value

string

in Net_SSH2 at line 1334
public _key_exchange(string $kexinit_payload_server = false)

Key Exchange

Parameters

string $kexinit_payload_server optional

in Net_SSH2 at line 1960
public int|null _encryption_algorithm_to_key_size(string $algorithm)

Maps an encryption algorithm name to the number of key bytes.

Parameters

string $algorithm Name of the encryption algorithm

Return Value

int|null Number of bytes as an integer or null for unknown

in Net_SSH2 at line 2004
public mixed _encryption_algorithm_to_crypt_instance(string $algorithm)

Maps an encryption algorithm name to an instance of a subclass of \phpseclib\Crypt\Base.

Parameters

string $algorithm Name of the encryption algorithm

Return Value

mixed Instance of \phpseclib\Crypt\Base or null for unknown

in Net_SSH2 at line 2088
public bool _bad_algorithm_candidate(string $algorithm)

Tests whether or not proposed algorithm has a potential for issues

Parameters

string $algorithm Name of the encryption algorithm

Return Value

bool

at line 465
public bool login($username)

Login

Parameters

$username

Return Value

bool

in Net_SSH2 at line 2129
public bool _login($username)

Login Helper

Parameters

$username

Return Value

bool

See also

self::_login_helper()

in Net_SSH2 at line 2160
public bool _login_helper(string $username, string $password = null)

Login Helper

Parameters

string $username
string $password

Return Value

bool

in Net_SSH2 at line 2364
public bool _keyboard_interactive_login(string $username, string $password)

Login via keyboard-interactive authentication

See {@link http://tools.ietf.org/html/rfc4256 RFC4256} for details. This is not a full-featured keyboard-interactive authenticator.

Parameters

string $username
string $password

Return Value

bool

in Net_SSH2 at line 2395
public bool _keyboard_interactive_process()

Handle the keyboard-interactive requests / responses.

Return Value

bool

in Net_SSH2 at line 2529
public bool _ssh_agent_login(string $username, System_SSH_Agent $agent)

Login with an ssh-agent provided key

Parameters

string $username
System_SSH_Agent $agent

Return Value

bool

in Net_SSH2 at line 2552
public bool _privatekey_login($username, $privatekey)

Login with an RSA private key

Parameters

$username
$privatekey

Return Value

bool

in Net_SSH2 at line 2682
public setTimeout(mixed $timeout)

Set Timeout

$ssh->exec('ping 127.0.0.1'); on a Linux host will never return and will run indefinitely. setTimeout() makes it so it'll timeout. Setting $timeout to false or 0 will mean there is no timeout.

Parameters

mixed $timeout

in Net_SSH2 at line 2692
public getStdError()

Get the output from stdError

in Net_SSH2 at line 2708
public string exec(string $command, Callback $callback = null)

Execute Command

If $callback is set to false then NetSSH2::getchannelpacket(NETSSH2CHANNEL_EXEC) will need to be called manually. In all likelihood, this is not a feature you want to be taking advantage of.

Parameters

string $command
Callback $callback

Return Value

string

in Net_SSH2 at line 2865
public bool _initShell()

Creates an interactive shell

Return Value

bool

See also

self::read()
self::write()

in Net_SSH2 at line 2973
public int _get_interactive_channel()

Return the channel to be used with read() / write()

Return Value

int

See also

self::read()
self::write()

in Net_SSH2 at line 2991
public int _get_open_channel()

Return an available open channel

Return Value

int

in Net_SSH2 at line 3015
public string read(string $expect = '', int $mode = NET_SSH2_READ_SIMPLE)

Returns the output of an interactive shell

Returns when there's a match for $expect, which can take the form of a string literal or, if $mode == NETSSH2READ_REGEX, a regular expression.

Parameters

string $expect
int $mode

Return Value

string

See also

self::write()

in Net_SSH2 at line 3064
public bool write(string $cmd)

Inputs a command into an interactive shell.

Parameters

string $cmd

Return Value

bool

See also

self::read()

in Net_SSH2 at line 3093
public bool startSubsystem(string $subsystem)

Start a subsystem.

Right now only one subsystem at a time is supported. To support multiple subsystem's stopSubsystem() could accept a string that contained the name of the subsystem, but at that point, only one subsystem of each type could be opened. To support multiple subsystem's of the same name maybe it'd be best if startSubsystem() generated a new channel id and returns that and then that that was passed into stopSubsystem() but that'll be saved for a future date and implemented if there's sufficient demand for such a feature.

Parameters

string $subsystem

Return Value

bool

See also

self::stopSubsystem()

in Net_SSH2 at line 3155
public bool stopSubsystem()

Stops a subsystem.

Return Value

bool

See also

self::startSubsystem()

in Net_SSH2 at line 3169
public reset()

Closes a channel

If read() timed out you might want to just close the channel and have it auto-restart on the next read() call

in Net_SSH2 at line 3181
public isTimeout()

Is timeout?

Did exec() or read() return because they timed out or because they encountered the end?

in Net_SSH2 at line 3191
public disconnect()

Disconnect

in Net_SSH2 at line 3207
public __destruct()

Destructor.

Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call disconnect().

in Net_SSH2 at line 3218
public bool isConnected()

Is the connection still active?

Return Value

bool

in Net_SSH2 at line 3229
public bool isAuthenticated()

Have you successfully been logged in?

Return Value

bool

in Net_SSH2 at line 3242
public bool ping()

Pings a server connection, or tries to reconnect if the connection has gone down

Inspired by http://php.net/manual/en/mysqli.ping.php

Return Value

bool

in Net_SSH2 at line 3284
public boolean _reconnect()

In situ reconnect method

Return Value

boolean

at line 3042
public _reset_connection(int $reason)

Resets a connection for re-use

Parameters

int $reason

in Net_SSH2 at line 3324
public string _get_binary_packet($skip_channel_filter = false)

Gets Binary Packets

See '6. Binary Packet Protocol' of rfc4253 for more info.

Parameters

$skip_channel_filter

Return Value

string

See also

self::_send_binary_packet()

in Net_SSH2 at line 3426
public string _filter($payload, $skip_channel_filter)

Filter Binary Packets

Because some binary packets need to be ignored...

Parameters

$payload
$skip_channel_filter

Return Value

string

See also

self::_get_binary_packet()

in Net_SSH2 at line 3587
public enableQuietMode()

Enable Quiet Mode

Suppress stderr from output

in Net_SSH2 at line 3599
public disableQuietMode()

Disable Quiet Mode

Show stderr in output

in Net_SSH2 at line 3613
public bool isQuietModeEnabled()

Returns whether Quiet Mode is enabled or not

Return Value

bool

See also

self::enableQuietMode()
self::disableQuietMode()

in Net_SSH2 at line 3623
public enablePTY()

Enable request-pty when using exec()

in Net_SSH2 at line 3633
public disablePTY()

Disable request-pty when using exec()

in Net_SSH2 at line 3651
public bool isPTYEnabled()

Returns whether request-pty is enabled or not

Return Value

bool

See also

self::enablePTY()
self::disablePTY()

in Net_SSH2 at line 3665
public mixed _get_channel_packet($client_channel, $skip_extended = false)

Gets channel data

Returns the data as a string if it's available and false if not.

Parameters

$client_channel
$skip_extended

Return Value

mixed

in Net_SSH2 at line 3935
public bool _send_binary_packet(string $data, string $logged = null)

Sends Binary Packets

See '6. Binary Packet Protocol' of rfc4253 for more info.

Parameters

string $data
string $logged

Return Value

bool

See also

self::_get_binary_packet()

in Net_SSH2 at line 3993
public _append_log($message_number, $message)

Logs data packets

Makes sure that only the last 1MB worth of packets will be logged

Parameters

$message_number
$message

in Net_SSH2 at line 4071
public bool _send_channel_packet(int $client_channel, string $data)

Sends channel data

Spans multiple SSHMSGCHANNEL_DATAs if appropriate

Parameters

int $client_channel
string $data

Return Value

bool

in Net_SSH2 at line 4119
public bool _close_channel(int $client_channel, bool $want_reply = false)

Closes and flushes a channel

Net_SSH2 doesn't properly close most channels. For exec() channels are normally closed by the server and for SFTP channels are presumably closed when the client disconnects. This functions is intended for SCP more than anything.

Parameters

int $client_channel
bool $want_reply

Return Value

bool

at line 3221
public bool _disconnect(int $reason)

Disconnect

Parameters

int $reason

Return Value

bool

in Net_SSH2 at line 4177
public string _string_shift(string $string, int $index = 1)

String Shift

Inspired by array_shift

Parameters

string $string
int $index

Return Value

string

in Net_SSH2 at line 4194
public _define_array()

Define Array

Takes any number of arrays whose indices are integers and whose values are strings and defines a bunch of named constants from it, using the value as the name of the constant and the index as the value of the constant. If any of the constants that would be defined already exists, none of the constants will be defined.

in Net_SSH2 at line 4216
public array|false|string getLog()

Returns a log of the packets that have been sent and received.

Returns a string if NETSSH2LOGGING == NETSSH2LOGCOMPLEX, an array if NETSSH2LOGGING == NETSSH2LOGSIMPLE and false if !defined('NETSSH2LOGGING')

Return Value

array|false|string

in Net_SSH2 at line 4241
public string _format_log(array $message_log, array $message_number_log)

Formats a log for printing

Parameters

array $message_log
array $message_number_log

Return Value

string

in Net_SSH2 at line 4276
public string _format_log_helper(array $matches)

Helper function for formatlog

For use with pregreplacecallback()

Parameters

array $matches

Return Value

string

in Net_SSH2 at line 4290
public _on_channel_open()

Helper function for agent->onchannel_open()

Used when channels are created to inform agent of said channel opening. Must be called after channel open confirmation received

in Net_SSH2 at line 4306
public mixed _array_intersect_first(array $array1, array $array2)

Returns the first value of the intersection of two arrays or false if the intersection is empty.

The order is defined by the first parameter.

Parameters

array $array1
array $array2

Return Value

mixed False if intersection is empty, else intersected value.

in Net_SSH2 at line 4322
public string[] getErrors()

Returns all errors

Return Value

string[]

in Net_SSH2 at line 4333
public string getLastError()

Returns the last error

Return Value

string

in Net_SSH2 at line 4348
public string getServerIdentification()

Return the server identification.

Return Value

string

in Net_SSH2 at line 4361
public array getKexAlgorithms()

Return a list of the key exchange algorithms the server supports.

Return Value

array

in Net_SSH2 at line 4374
public array getServerHostKeyAlgorithms()

Return a list of the host key (public key) algorithms the server supports.

Return Value

array

in Net_SSH2 at line 4387
public array getEncryptionAlgorithmsClient2Server()

Return a list of the (symmetric key) encryption algorithms the server supports, when receiving stuff from the client.

Return Value

array

in Net_SSH2 at line 4400
public array getEncryptionAlgorithmsServer2Client()

Return a list of the (symmetric key) encryption algorithms the server supports, when sending stuff to the client.

Return Value

array

in Net_SSH2 at line 4413
public array getMACAlgorithmsClient2Server()

Return a list of the MAC algorithms the server supports, when receiving stuff from the client.

Return Value

array

in Net_SSH2 at line 4426
public array getMACAlgorithmsServer2Client()

Return a list of the MAC algorithms the server supports, when sending stuff to the client.

Return Value

array

in Net_SSH2 at line 4439
public array getCompressionAlgorithmsClient2Server()

Return a list of the compression algorithms the server supports, when receiving stuff from the client.

Return Value

array

in Net_SSH2 at line 4452
public array getCompressionAlgorithmsServer2Client()

Return a list of the compression algorithms the server supports, when sending stuff to the client.

Return Value

array

in Net_SSH2 at line 4465
public array getLanguagesServer2Client()

Return a list of the languages the server supports, when sending stuff to the client.

Return Value

array

in Net_SSH2 at line 4478
public array getLanguagesClient2Server()

Return a list of the languages the server supports, when receiving stuff from the client.

Return Value

array

in Net_SSH2 at line 4491
public array getServerAlgorithms()

Returns a list of algorithms the server supports

Return Value

array

in Net_SSH2 at line 4519
public array getSupportedKEXAlgorithms()

Returns a list of KEX algorithms that phpseclib supports

Return Value

array

in Net_SSH2 at line 4540
public array getSupportedHostKeyAlgorithms()

Returns a list of host key algorithms that phpseclib supports

Return Value

array

in Net_SSH2 at line 4556
public array getSupportedEncryptionAlgorithms()

Returns a list of symmetric key algorithms that phpseclib supports

Return Value

array

in Net_SSH2 at line 4634
public array getSupportedMACAlgorithms()

Returns a list of MAC algorithms that phpseclib supports

Return Value

array

in Net_SSH2 at line 4654
public array getSupportedCompressionAlgorithms()

Returns a list of compression algorithms that phpseclib supports

Return Value

array

in Net_SSH2 at line 4670
public array getAlgorithmsNegotiated()

Return list of negotiated algorithms

Uses the same format as https://www.php.net/ssh2-methods-negotiated

Return Value

array

in Net_SSH2 at line 4697
public setPreferredAlgorithms(array $methods)

Accepts an associative array with up to four parameters as described at https://www.php.net/manual/en/function.ssh2-connect.php

Parameters

array $methods

in Net_SSH2 at line 4785
public string getBannerMessage()

Returns the banner message.

Quoting from the RFC, "in some jurisdictions, sending a warning message before authentication may be relevant for getting legal protection."

Return Value

string

in Net_SSH2 at line 4799
public mixed getServerPublicHostKey()

Returns the server public host key.

Caching this the first time you connect to a server and checking the result on subsequent connections is recommended. Returns false if the server signature is not signed correctly with the public host key.

Return Value

mixed

in Net_SSH2 at line 5008
public false|int getExitStatus()

Returns the exit status of an SSH command or false.

Return Value

false|int

in Net_SSH2 at line 5022
public int getWindowColumns()

Returns the number of columns for the terminal window size.

Return Value

int

in Net_SSH2 at line 5033
public int getWindowRows()

Returns the number of rows for the terminal window size.

Return Value

int

in Net_SSH2 at line 5044
public setWindowColumns(int $value)

Sets the number of columns for the terminal window size.

Parameters

int $value

in Net_SSH2 at line 5055
public setWindowRows(int $value)

Sets the number of rows for the terminal window size.

Parameters

int $value

in Net_SSH2 at line 5067
public setWindowSize(int $columns = 80, int $rows = 24)

Sets the number of columns and rows for the terminal window size.

Parameters

int $columns
int $rows

at line 452
public Net_SFTP(string $host, int $port = 22, int $timeout = 10)

PHP4 compatible Default Constructor.

Parameters

string $host
int $port
int $timeout

See also

self::__construct()

at line 632
public disableStatCache()

Disable the stat cache

at line 642
public enableStatCache()

Enable the stat cache

at line 652
public clearStatCache()

Clear the stat cache

at line 662
public enablePathCanonicalization()

Enable path canonicalization

at line 672
public disablePathCanonicalization()

Enable path canonicalization

at line 683
public mixed pwd()

Returns the current directory name

Return Value

mixed

at line 695
public _logError(string $response, int $status = -1)

Logs errors

Parameters

string $response
int $status

at line 724
public mixed realpath(string $path)

Returns canonicalized absolute pathname

realpath() expands all symbolic links and resolves references to '/./', '/../' and extra '/' characters in the input path and returns the canonicalized absolute pathname.

Parameters

string $path

Return Value

mixed

at line 743
public mixed _realpath(string $path)

Canonicalize the Server-Side Path Name

SFTP doesn't provide a mechanism by which the current working directory can be changed, so we'll emulate it. Returns the absolute (canonicalized) path.

If canonicalize_paths has been disabled using disablePathCanonicalization(), $path is returned as-is.

Parameters

string $path

Return Value

mixed

See also

self::chdir()
self::disablePathCanonicalization()

at line 806
public bool chdir(string $dir)

Changes the current directory

Parameters

string $dir

Return Value

bool

at line 869
public mixed nlist(string $dir = '.', bool $recursive = false)

Returns a list of files in the given directory

Parameters

string $dir
bool $recursive

Return Value

mixed

at line 883
public mixed _nlist_helper(string $dir, bool $recursive, string $relativeDir)

Helper method for nlist

Parameters

string $dir
bool $recursive
string $relativeDir

Return Value

mixed

at line 919
public mixed rawlist(string $dir = '.', bool $recursive = false)

Returns a detailed list of files in the given directory

Parameters

string $dir
bool $recursive

Return Value

mixed

at line 963
public mixed _list(string $dir, bool $raw = true)

Reads a list, be it detailed or not, of files in the given directory

Parameters

string $dir
bool $raw

Return Value

mixed

at line 1085
public int _comparator(array $a, array $b)

Compares two rawlist entries using parameters set by setListOrder()

Intended for use with uasort()

Parameters

array $a
array $b

Return Value

int

at line 1159
public setListOrder()

Defines how nlist() and rawlist() will be sorted - if at all.

If sorting is enabled directories and files will be sorted independently with directories appearing before files in the resultant array that is returned.

Any parameter returned by stat is a valid sort parameter for this function. Filename comparisons are case insensitive.

Examples:

$sftp->setListOrder('filename', SORTASC); $sftp->setListOrder('size', SORTDESC, 'filename', SORT_ASC); $sftp->setListOrder(true); Separates directories from files but doesn't do any sorting beyond that $sftp->setListOrder(); Don't do any sort of sorting

at line 1184
public mixed size(string $filename)

Returns the file size, in bytes, or false, on failure

Files larger than 4GB will show up as being exactly 4GB.

Parameters

string $filename

Return Value

mixed

at line 1204
public _update_stat_cache(string $path, mixed $value)

Save files / directories to cache

Parameters

string $path
mixed $value

at line 1248
public bool _remove_from_stat_cache(string $path)

Remove files / directories from cache

Parameters

string $path

Return Value

bool

at line 1275
public mixed _query_stat_cache($path)

Checks cache for path

Mainly used by file_exists

Parameters

$path

Return Value

mixed

at line 1298
public mixed stat(string $filename)

Returns general information about a file.

Returns an array on success and false otherwise.

Parameters

string $filename

Return Value

mixed

at line 1355
public mixed lstat(string $filename)

Returns general information about a file or symbolic link.

Returns an array on success and false otherwise.

Parameters

string $filename

Return Value

mixed

at line 1422
public mixed _stat(string $filename, int $type)

Returns general information about a file or symbolic link

Determines information without calling NetSFTP::realpath(). The second parameter can be either NETSFTPSTAT or NETSFTP_LSTAT.

Parameters

string $filename
int $type

Return Value

mixed

at line 1451
public bool truncate(string $filename, int $new_size)

Truncates a file to a given length

Parameters

string $filename
int $new_size

Return Value

bool

at line 1469
public bool touch(string $filename, int $time = null, int $atime = null)

Sets access and modification time of file.

If the file does not exist, it will be created.

Parameters

string $filename
int $time
int $atime

Return Value

bool

at line 1520
public bool chown(string $filename, int $uid, bool $recursive = false)

Changes file or directory owner

Returns true on success or false on error.

Parameters

string $filename
int $uid
bool $recursive

Return Value

bool

at line 1540
public bool chgrp(string $filename, int $gid, bool $recursive = false)

Changes file or directory group

Returns true on success or false on error.

Parameters

string $filename
int $gid
bool $recursive

Return Value

bool

at line 1559
public mixed chmod(int $mode, string $filename, bool $recursive = false)

Set permissions on a file.

Returns the new file permissions on success or false on error. If $recursive is true than this just returns true or false.

Parameters

int $mode
string $filename
bool $recursive

Return Value

mixed

at line 1607
public bool _setstat(string $filename, string $attr, bool $recursive)

Sets information about a file

Parameters

string $filename
string $attr
bool $recursive

Return Value

bool

at line 1669
public bool _setstat_recursive(string $path, string $attr, int $i)

Recursively sets information on directories on the SFTP server

Minimizes directory lookups and SSHFXPSTATUS requests for speed.

Parameters

string $path
string $attr
int $i

Return Value

bool

Return the target of a symbolic link

Parameters

string $link

Return Value

mixed

Create a symlink

symlink() creates a symbolic link to the existing target with the specified name link.

Parameters

string $target
string $link

Return Value

bool

at line 1826
public bool mkdir($dir, $mode = -1, $recursive = false)

Creates a directory.

Parameters

$dir
$mode
$recursive

Return Value

bool

at line 1861
public bool _mkdir_helper($dir, $attr)

Helper function for directory creation

Parameters

$dir
$attr

Return Value

bool

at line 1892
public bool rmdir(string $dir)

Removes a directory.

Parameters

string $dir

Return Value

bool

at line 1979
public bool put(string $remote_file, string|resource $data, int $mode = NET_SFTP_STRING, int $start = -1, int $local_start = -1, callable|null $progressCallback = null)

Uploads a file to the SFTP server.

By default, NetSFTP::put() does not read from the local filesystem. $data is dumped directly into $remotefile. So, for example, if you set $data to 'filename.ext' and then do Net_SFTP::get(), you will get a file, twelve bytes long, containing 'filename.ext' as its contents.

Setting $mode to NETSFTPLOCALFILE will change the above behavior. With NETSFTPLOCALFILE, $remotefile will contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how large $remotefile will be, as well.

If $data is a resource then it'll be used as a resource instead.

Setting $mode to NETSFTPCALLBACK will use $data as callback function, which gets only one parameter -- number of bytes to return, and returns a string if there is some data or null if there is no more data

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.

$mode can take an additional two parameters - NETSFTPRESUME and NETSFTPRESUME_START. These are bitwise AND'd with $mode. So if you want to resume upload of a 300mb file on the local file system you'd set $mode to the following:

NETSFTPLOCALFILE | NETSFTP_RESUME

If you wanted to simply append the full contents of a local file to the full contents of a remote file you'd replace NETSFTPRESUME with NETSFTPRESUME_START.

If $mode & (NETSFTPRESUME | NETSFTPRESUMESTART) then NETSFTPRESUMESTART will be assumed.

$start and $localstart give you more fine grained control over this process and take precident over NETSFTPRESUME when they're non-negative. ie. $start could let you write at the end of a file (like NETSFTPRESUME) or in the middle of one. $localstart could let you start your reading from the end of a file (like NETSFTPRESUME_START) or in the middle of one.

Setting $localstart to > 0 or $mode | NETSFTPRESUMESTART doesn't do anything unless $mode | NETSFTPLOCAL_FILE.

Parameters

string $remote_file
string|resource $data
int $mode
int $start
int $local_start
callable|null $progressCallback

Return Value

bool

at line 2141
public bool _read_put_responses(int $i)

Reads multiple successive SSHFXPWRITE responses

Sending an SSHFXPWRITE packet and immediately reading its response isn't as efficient as blindly sending out $i SSHFXPWRITEs, in succession, and then reading $i responses.

Parameters

int $i

Return Value

bool

at line 2170
public bool _close_handle(string $handle)

Close handle

Parameters

string $handle

Return Value

bool

at line 2213
public mixed get(string $remote_file, string $local_file = false, int $offset, int $length = -1, callable|null $progressCallback = null)

Downloads a file from the SFTP server.

Returns a string containing the contents of $remotefile if $localfile 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.

Parameters

string $remote_file
string $local_file
int $offset
int $length
callable|null $progressCallback

Return Value

mixed

at line 2360
public bool delete(string $path, bool $recursive = true)

Deletes a file on the SFTP server.

Parameters

string $path
bool $recursive

Return Value

bool

at line 2422
public bool _delete_recursive(string $path, int $i)

Recursively deletes directories on the SFTP server

Minimizes directory lookups and SSHFXPSTATUS requests for speed.

Parameters

string $path
int $i

Return Value

bool

at line 2488
public bool file_exists(string $path)

Checks whether a file or directory exists

Parameters

string $path

Return Value

bool

at line 2511
public bool is_dir(string $path)

Tells whether the filename is a directory

Parameters

string $path

Return Value

bool

at line 2527
public bool is_file(string $path)

Tells whether the filename is a regular file

Parameters

string $path

Return Value

bool

Tells whether the filename is a symbolic link

Parameters

string $path

Return Value

bool

at line 2559
public bool is_readable(string $path)

Tells whether a file exists and is readable

Parameters

string $path

Return Value

bool

at line 2587
public bool is_writable(string $path)

Tells whether the filename is writable

Parameters

string $path

Return Value

bool

at line 2617
public bool is_writeable(string $path)

Tells whether the filename is writeable

Alias of is_writable

Parameters

string $path

Return Value

bool

at line 2629
public mixed fileatime(string $path)

Gets last access time of file

Parameters

string $path

Return Value

mixed

at line 2641
public mixed filemtime(string $path)

Gets file modification time

Parameters

string $path

Return Value

mixed

at line 2653
public mixed fileperms(string $path)

Gets file permissions

Parameters

string $path

Return Value

mixed

at line 2665
public mixed fileowner(string $path)

Gets file owner

Parameters

string $path

Return Value

mixed

at line 2677
public mixed filegroup(string $path)

Gets file group

Parameters

string $path

Return Value

mixed

at line 2689
public mixed filesize(string $path)

Gets file size

Parameters

string $path

Return Value

mixed

at line 2701
public mixed filetype(string $path)

Gets file type

Parameters

string $path

Return Value

mixed

at line 2736
public mixed _get_stat_cache_prop(string $path, string $prop)

Return a stat properity

Uses cache if appropriate.

Parameters

string $path
string $prop

Return Value

mixed

at line 2751
public mixed _get_lstat_cache_prop(string $path, string $prop)

Return an lstat properity

Uses cache if appropriate.

Parameters

string $path
string $prop

Return Value

mixed

at line 2766
public mixed _get_xstat_cache_prop($path, $prop, $type)

Return a stat or lstat properity

Uses cache if appropriate.

Parameters

$path
$prop
$type

Return Value

mixed

at line 2795
public bool rename(string $oldname, string $newname)

Renames a file or a directory on the SFTP server

Parameters

string $oldname
string $newname

Return Value

bool

at line 2847
public array _parseAttributes(string $response)

Parse Attributes

See '7. File Attributes' of draft-ietf-secsh-filexfer-13 for more info.

Parameters

string $response

Return Value

array

at line 2929
public int _parseMode(int $mode)

Attempt to identify the file type

Quoting the SFTP RFC, "Implementations MUST NOT send bits that are not defined" but they seem to anyway

Parameters

int $mode

Return Value

int

at line 2976
public mixed _parseLongname(string $longname)

Parse Longname

SFTPv3 doesn't provide any easy way of identifying a file type. You could try to open a file as a directory and see if an error is returned or you could try to parse the SFTPv3-specific longname field of the SSHFXPNAME packet. That's what this function does. The result is returned using the {@link http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-5.2 SFTPv4 type constants}.

If the longname is in an unrecognized format bool(false) is returned.

Parameters

string $longname

Return Value

mixed

at line 3008
public bool _send_sftp_packet($type, $data, $request_id = 1)

Sends SFTP Packets

See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info.

Parameters

$type
$data
$request_id

Return Value

bool

See also

self::_get_sftp_packet()
Net_SSH2::_send_channel_packet()

at line 3063
public string _get_sftp_packet($request_id = null)

Receives SFTP Packets

See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info.

Incidentally, the number of SSHMSGCHANNELDATA messages has no bearing on the number of SFTP packets present. There can be one SSHMSGCHANNELDATA messages containing two SFTP packets or there can be two SSHMSGCHANNEL_DATA messages containing one SFTP packet.

Parameters

$request_id

Return Value

string

See also

self::_send_sftp_packet()

at line 3161
public string getSFTPLog()

Returns a log of the packets that have been sent and received.

Returns a string if NETSFTPLOGGING == NETSFTPLOGCOMPLEX, an array if NETSFTPLOGGING == NETSFTPLOGSIMPLE and false if !defined('NETSFTPLOGGING')

Return Value

string or Array

at line 3183
public array getSFTPErrors()

Returns all errors

Return Value

array

at line 3194
public string getLastSFTPError()

Returns the last error

Return Value

string

at line 3205
public array getSupportedVersions()

Get supported SFTP versions

Return Value

array