Stream

SFTP Stream Wrapper

author

Jim Wigginton terrafrost@php.net

package

Default

Methods

__call Magic Method

__call(string $name,array $arguments): mixed

When you're utilizing an SFTP stream you're not calling the methods in this class directly - PHP is calling them for you. Which kinda begs the question... what methods is PHP calling and what parameters is it passing to them? This function lets you figure that out.

If NET_SFTP_STREAM_LOGGING is defined all calls will be output on the screen and then (regardless of whether or not NET_SFTP_STREAM_LOGGING is enabled) the parameters will be passed through to the appropriate method.

Arguments

$name

string

$arguments

array

Response

mixed

The Constructor

__construct()

Close directory handle

_dir_closedir(): boolean

Response

boolean

Open directory handle

_dir_opendir(string $path,integer $options): boolean

The only $options is "whether or not to enforce safe_mode (0x04)". Since safe mode was deprecated in 5.3 and removed in 5.4 I'm just going to ignore it.

Also, nlist() is the best that this function is realistically going to be able to do. When an SFTP client sends a SSH_FXP_READDIR packet you don't generally get info on just one file but on multiple files. Quoting the SFTP specs:

The SSH_FXP_NAME response has the following format:

   uint32     id
   uint32     count
   repeats count times:
           string     filename
           string     longname
           ATTRS      attrs

Arguments

$path

string

$options

integer

Response

boolean

Read entry from directory handle

_dir_readdir(): mixed

Response

mixed

Rewind directory handle

_dir_rewinddir(): boolean

Response

boolean

Create a directory

_mkdir(string $path,integer $mode,integer $options): boolean

Only valid $options is STREAM_MKDIR_RECURSIVE

Arguments

$path

string

$mode

integer

$options

integer

Response

boolean

Renames a file or directory

_rename(string $path_from,string $path_to): boolean

Attempts to rename oldname to newname, moving it between directories if necessary. If newname exists, it will be overwritten. This is a departure from what \phpseclib3\Net\SFTP does.

Arguments

$path_from

string

$path_to

string

Response

boolean

Removes a directory

_rmdir(string $path,integer $options): boolean

Only valid $options is STREAM_MKDIR_RECURSIVE per http://php.net/streamwrapper.rmdir, however, http://php.net/rmdir does not have a $recursive parameter as mkdir() does so I don't know how STREAM_MKDIR_RECURSIVE is supposed to be set. Also, when I try it out with rmdir() I get 8 as $options. What does 8 correspond to?

Arguments

$path

string

$options

integer

Response

boolean

Retrieve the underlaying resource

_stream_cast(integer $cast_as): resource

Arguments

$cast_as

integer

Response

resource

Close an resource

_stream_close()

Tests for end-of-file on a file pointer

_stream_eof(): boolean

In my testing there are four classes functions that normally effect the pointer: fseek, fputs / fwrite, fgets / fread and ftruncate.

Only fgets / fread, however, results in feof() returning true. do fputs($fp, 'aaa') on a blank file and feof() will return false. do fread($fp, 1) and feof() will then return true. do fseek($fp, 10) on ablank file and feof() will return false. do fread($fp, 1) and feof() will then return true.

Response

boolean

Flushes the output

_stream_flush(): boolean

See http://php.net/fflush. Always returns true because \phpseclib3\Net\SFTP doesn't cache stuff before writing

Response

boolean

Advisory file locking

_stream_lock(integer $operation): boolean

Arguments

$operation

integer

Response

boolean

Opens file or URL

_stream_open(string $path,string $mode,integer $options,string &$opened_path): boolean

Arguments

$path

string

$mode

string

$options

integer

$opened_path

string

Response

boolean

Read from stream

_stream_read(integer $count): mixed

Arguments

$count

integer

Response

mixed

Seeks to specific location in a stream

_stream_seek(integer $offset,integer $whence): boolean

Arguments

$offset

integer

$whence

integer

Response

boolean

Change stream options

_stream_set_option(integer $option,integer $arg1,integer $arg2): boolean

STREAM_OPTION_WRITE_BUFFER isn't supported for the same reason stream_flush isn't. The other two aren't supported because of limitations in \phpseclib3\Net\SFTP.

Arguments

$option

integer

$arg1

integer

$arg2

integer

Response

boolean

Retrieve information about a file resource

_stream_stat(): mixed

Response

mixed

Retrieve the current position of a stream

_stream_tell(): integer

Response

integer

Truncate stream

_stream_truncate(integer $new_size): boolean

Arguments

$new_size

integer

Response

boolean

Write to stream

_stream_write(string $data): integer|false

Arguments

$data

string

Response

integer|false

Retrieve information about a file

_url_stat(string $path,integer $flags): mixed

Ignores the STREAM_URL_STAT_QUIET flag because the entirety of \phpseclib3\Net\SFTP\Stream is quiet by default might be worthwhile to reconstruct bits 12-16 (ie. the file type) if mode doesn't have them but we'll cross that bridge when and if it's reached

Arguments

$path

string

$flags

integer

Response

mixed

Path Parser

parse_path(string $path): string

Extract a path from a URI and actually connect to an SSH server if appropriate

If "notification" is set as a context parameter the message code for successful login is NET_SSH2_MSG_USERAUTH_SUCCESS. For a failed login it's NET_SSH2_MSG_USERAUTH_FAILURE.

Arguments

$path

string

Response

string

Registers this class as a URL wrapper.

register(string $protocol = 'sftp'): boolean
static

Arguments

$protocol

string

The wrapper name to be registered.

Response

boolean

True on success, false otherwise.

Properties

SFTP instances

instances :array
static

Rather than re-create the connection we re-use instances if possible

var

Type(s)

array

SFTP instance

sftp :object
var

Type(s)

object

Path

path :string
var

Type(s)

string

Mode

mode :string
var

Type(s)

string

Position

pos :integer
var

Type(s)

integer

Size

size :integer
var

Type(s)

integer

Directory entries

entries :array
var

Type(s)

array

EOF flag

eof :boolean
var

Type(s)

boolean

Context resource

context :resource

Technically this needs to be publicly accessible so PHP can set it directly

var

Type(s)

resource

Notification callback function

notification :callable
var

Type(s)

callable