phpseclib API Documentation
Class

Net_SFTP_Stream

class Net_SFTP_Stream

SFTP Stream Wrapper

Properties

object $sftp SFTP instance
string $path Path
string $mode Mode
int $pos Position
int $size Size
array $entries Directory entries
bool $eof EOF flag
resource $context Context resource
callable $notification Notification callback function

Methods

Net_SFTP_Stream()

The Constructor

string _parse_path(string $path)

Path Parser

bool _stream_open(string $path, string $mode, int $options, string $opened_path)

Opens file or URL

mixed _stream_read(int $count)

Read from stream

mixed _stream_write(string $data)

Write to stream

int _stream_tell()

Retrieve the current position of a stream

bool _stream_eof()

Tests for end-of-file on a file pointer

bool _stream_seek(int $offset, int $whence)

Seeks to specific location in a stream

bool _stream_metadata(string $path, int $option, mixed $var)

Change stream options

resource _stream_cast(int $cast_as)

Retrieve the underlaying resource

bool _stream_lock(int $operation)

Advisory file locking

bool _rename(string $path_from, string $path_to)

Renames a file or directory

bool _dir_opendir(string $path, int $options)

Open directory handle

mixed _dir_readdir()

Read entry from directory handle

bool _dir_rewinddir()

Rewind directory handle

bool _dir_closedir()

Close directory handle

bool _mkdir(string $path, int $mode, int $options)

Create a directory

bool _rmdir($path, $options)

Removes a directory

bool _stream_flush()

Flushes the output

mixed _stream_stat()

Retrieve information about a file resource

bool _unlink(string $path)

Delete a file

mixed _url_stat(string $path, int $flags)

Retrieve information about a file

bool _stream_truncate(int $new_size)

Truncate stream

bool _stream_set_option(int $option, int $arg1, int $arg2)

Change stream options

_stream_close()

Close an resource

mixed __call(string $name, array $arguments)

__call Magic Method

Details

at line 149
public Net_SFTP_Stream()

The Constructor

at line 172
public string _parse_path(string $path)

Path Parser

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 NETSSH2MSGUSERAUTHSUCCESS. For a failed login it's NETSSH2MSGUSERAUTHFAILURE.

Parameters

string $path

Return Value

string

at line 278
public bool _stream_open(string $path, string $mode, int $options, string $opened_path)

Opens file or URL

Parameters

string $path
string $mode
int $options
string $opened_path

Return Value

bool

at line 320
public mixed _stream_read(int $count)

Read from stream

Parameters

int $count

Return Value

mixed

at line 362
public mixed _stream_write(string $data)

Write to stream

Parameters

string $data

Return Value

mixed

at line 396
public int _stream_tell()

Retrieve the current position of a stream

Return Value

int

at line 414
public bool _stream_eof()

Tests for end-of-file on a file pointer

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.

Return Value

bool

at line 427
public bool _stream_seek(int $offset, int $whence)

Seeks to specific location in a stream

Parameters

int $offset
int $whence

Return Value

bool

at line 456
public bool _stream_metadata(string $path, int $option, mixed $var)

Change stream options

Parameters

string $path
int $option
mixed $var

Return Value

bool

at line 488
public resource _stream_cast(int $cast_as)

Retrieve the underlaying resource

Parameters

int $cast_as

Return Value

resource

at line 500
public bool _stream_lock(int $operation)

Advisory file locking

Parameters

int $operation

Return Value

bool

at line 517
public bool _rename(string $path_from, string $path_to)

Renames a file or directory

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 Net_SFTP does.

Parameters

string $path_from
string $path_to

Return Value

bool

at line 569
public bool _dir_opendir(string $path, int $options)

Open directory handle

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 SSHFXPREADDIR packet you don't generally get info on just one file but on multiple files. Quoting the SFTP specs:

The SSHFXPNAME response has the following format:

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

Parameters

string $path
int $options

Return Value

bool

at line 586
public mixed _dir_readdir()

Read entry from directory handle

Return Value

mixed

at line 600
public bool _dir_rewinddir()

Rewind directory handle

Return Value

bool

at line 612
public bool _dir_closedir()

Close directory handle

Return Value

bool

at line 628
public bool _mkdir(string $path, int $mode, int $options)

Create a directory

Only valid $options is STREAMMKDIRRECURSIVE

Parameters

string $path
int $mode
int $options

Return Value

bool

at line 652
public bool _rmdir($path, $options)

Removes a directory

Only valid $options is STREAMMKDIRRECURSIVE 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 STREAMMKDIRRECURSIVE is supposed to be set. Also, when I try it out with rmdir() I get 8 as $options. What does 8 correspond to?

Parameters

$path
$options

Return Value

bool

at line 670
public bool _stream_flush()

Flushes the output

See http://php.net/fflush. Always returns true because Net_SFTP doesn't cache stuff before writing

Return Value

bool

at line 681
public mixed _stream_stat()

Retrieve information about a file resource

Return Value

mixed

Delete a file

Parameters

string $path

Return Value

bool

at line 719
public mixed _url_stat(string $path, int $flags)

Retrieve information about a file

Ignores the STREAMURLSTATQUIET flag because the entirety of NetSFTP_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

Parameters

string $path
int $flags

Return Value

mixed

at line 741
public bool _stream_truncate(int $new_size)

Truncate stream

Parameters

int $new_size

Return Value

bool

at line 765
public bool _stream_set_option(int $option, int $arg1, int $arg2)

Change stream options

STREAMOPTIONWRITEBUFFER isn't supported for the same reason streamflush isn't. The other two aren't supported because of limitations in Net_SFTP.

Parameters

int $option
int $arg1
int $arg2

Return Value

bool

at line 775
public _stream_close()

Close an resource

at line 794
public mixed __call(string $name, array $arguments)

__call Magic Method

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 NETSFTPSTREAMLOGGING is defined all calls will be output on the screen and then (regardless of whether or not NETSFTPSTREAMLOGGING is enabled) the parameters will be passed through to the appropriate method.

Parameters

string $name
array $arguments

Return Value

mixed