PHP Class phpseclib\Net\SFTP

Author: Jim Wigginton ([email protected])
Inheritance: extends SSH2
Mostra file Open project: phpseclib/phpseclib Class Usage Examples

Public Properties

Property Type Description
$extensions array Extensions supported by the server
$max_sftp_packet array Max SFTP Packet Size
$packet_buffer string Packet Buffer
$packet_log array Packet Log
$packet_type integer The request ID exists in the off chance that a packet is sent out-of-order. Of course, this library doesn't support concurrent actions, so it's somewhat academic, here.
$packet_type_log array Packet Type Log
$packet_types array Packet Types
$pwd string Current working directory
$request_id integer The request ID exists in the off chance that a packet is sent out-of-order. Of course, this library doesn't support concurrent actions, so it's somewhat academic, here.
$sftp_errors string Error information
$sortOptions array Sort Options
$stat_cache array Rather than always having to open a directory and close it immediately there after to see if a file is a directory we'll cache the results.
$status_codes array Status Codes
$use_stat_cache boolean Stat Cache Flag
$version integer Server SFTP version

Public Methods

Method Description
__construct ( string $host, integer $port = 22, integer $timeout = 10 ) : SFTP Default Constructor.
_close_handle ( string $handle ) : boolean Close handle
_comparator ( array $a, array $b ) : integer Compares two rawlist entries using parameters set by setListOrder()
_delete_recursive ( string $path, integer &$i ) : boolean Recursively deletes directories on the SFTP server
_disconnect ( integer $reason ) : boolean Disconnect
_get_lstat_cache_prop ( string $path, string $prop ) : mixed Return an lstat properity
_get_sftp_packet ( ) : string Receives SFTP Packets
_get_stat_cache_prop ( string $path, string $prop ) : mixed Return a stat properity
_get_xstat_cache_prop ( string $path, string $prop, $type ) : mixed Return a stat or lstat properity
_list ( string $dir, boolean $raw = true ) : mixed Reads a list, be it detailed or not, of files in the given directory
_logError ( string $response, integer $status ) Logs errors
_mkdir_helper ( string $dir, $attr ) : boolean Helper function for directory creation
_nlist_helper ( string $dir, boolean $recursive, string $relativeDir ) : mixed Helper method for nlist
_parseAttributes ( string &$response ) : array Parse Attributes
_parseLongname ( string $longname ) : mixed Parse Longname
_parseMode ( integer $mode ) : integer Attempt to identify the file type
_query_stat_cache ( $path ) : mixed Checks cache for path
_read_put_responses ( integer $i ) : boolean Reads multiple successive SSH_FXP_WRITE responses
_realpath ( string $path ) : mixed Canonicalize the Server-Side Path Name
_remove_from_stat_cache ( string $path ) : boolean Remove files / directories from cache
_send_sftp_packet ( integer $type, string $data ) : boolean Sends SFTP Packets
_setstat ( string $filename, string $attr, boolean $recursive ) : boolean Sets information about a file
_setstat_recursive ( string $path, string $attr, integer &$i ) : boolean Recursively sets information on directories on the SFTP server
_stat ( string $filename, integer $type ) : mixed Returns general information about a file or symbolic link
_update_stat_cache ( string $path, mixed $value ) Save files / directories to cache
chdir ( string $dir ) : boolean Changes the current directory
chgrp ( string $filename, integer $gid, boolean $recursive = false ) : boolean Changes file or directory group
chmod ( integer $mode, string $filename, boolean $recursive = false ) : mixed Set permissions on a file.
chown ( string $filename, integer $uid, boolean $recursive = false ) : boolean Changes file or directory owner
clearStatCache ( ) Clear the stat cache
delete ( string $path, boolean $recursive = true ) : boolean Deletes a file on the SFTP server.
disableStatCache ( ) Disable the stat cache
enableStatCache ( ) Enable the stat cache
file_exists ( string $path ) : boolean Checks whether a file or directory exists
fileatime ( string $path ) : mixed Gets last access time of file
filegroup ( string $path ) : mixed Gets file group
filemtime ( string $path ) : mixed Gets file modification time
fileowner ( string $path ) : mixed Gets file owner
fileperms ( string $path ) : mixed Gets file permissions
filesize ( string $path ) : mixed Gets file size
filetype ( string $path ) : mixed Gets file type
get ( string $remote_file, string $local_file = false, integer $offset, integer $length ) : mixed Downloads a file from the SFTP server.
getLastSFTPError ( ) : string Returns the last error
getSFTPErrors ( ) : string Returns all errors
getSFTPLog ( ) : string Returns a log of the packets that have been sent and received.
getSupportedVersions ( ) : array Get supported SFTP versions
is_dir ( string $path ) : boolean Tells whether the filename is a directory
is_file ( string $path ) : boolean Tells whether the filename is a regular file
is_link ( string $path ) : boolean Tells whether the filename is a symbolic link
is_readable ( string $path ) : boolean Tells whether a file exists and is readable
is_writable ( string $path ) : boolean Tells whether the filename is writable
is_writeable ( string $path ) : boolean Tells whether the filename is writeable
login ( string $username ) : boolean Login
lstat ( string $filename ) : mixed Returns general information about a file or symbolic link.
mkdir ( string $dir, $mode, $recursive = false ) : boolean Creates a directory.
nlist ( string $dir = '.', boolean $recursive = false ) : mixed Returns a list of files in the given directory
put ( string $remote_file, string | resource $data, integer $mode = self::SOURCE_STRING, integer $start, integer $local_start, callable | null $progressCallback = null ) : boolean Uploads a file to the SFTP server.
pwd ( ) : mixed Returns the current directory name
rawlist ( string $dir = '.', boolean $recursive = false ) : mixed Returns a detailed list of files in the given directory
readlink ( string $link ) : mixed Return the target of a symbolic link
realpath ( string $path ) : mixed Returns canonicalized absolute pathname
rename ( string $oldname, string $newname ) : boolean Renames a file or a directory on the SFTP server
rmdir ( string $dir ) : boolean Removes a directory.
setListOrder ( ) Defines how nlist() and rawlist() will be sorted - if at all.
size ( string $filename ) : mixed Returns the file size, in bytes, or false, on failure
stat ( string $filename ) : mixed Returns general information about a file.
symlink ( string $target, string $link ) : boolean Create a symlink
touch ( string $filename, integer $time = null, integer $atime = null ) : boolean Sets access and modification time of file.
truncate ( string $filename, integer $new_size ) : boolean Truncates a file to a given length

Method Details

__construct() public method

Connects to an SFTP server
public __construct ( string $host, integer $port = 22, integer $timeout = 10 ) : SFTP
$host string
$port integer
$timeout integer
return SFTP

_close_handle() public method

Close handle
public _close_handle ( string $handle ) : boolean
$handle string
return boolean

_comparator() public method

Intended for use with uasort()
public _comparator ( array $a, array $b ) : integer
$a array
$b array
return integer

_delete_recursive() public method

Minimizes directory lookups and SSH_FXP_STATUS requests for speed.
public _delete_recursive ( string $path, integer &$i ) : boolean
$path string
$i integer
return boolean

_disconnect() public method

Disconnect
public _disconnect ( integer $reason ) : boolean
$reason integer
return boolean

_get_lstat_cache_prop() public method

Uses cache if appropriate.
public _get_lstat_cache_prop ( string $path, string $prop ) : mixed
$path string
$prop string
return mixed

_get_sftp_packet() public method

See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info. Incidentally, the number of SSH_MSG_CHANNEL_DATA messages has no bearing on the number of SFTP packets present. There can be one SSH_MSG_CHANNEL_DATA messages containing two SFTP packets or there can be two SSH_MSG_CHANNEL_DATA messages containing one SFTP packet.
See also: self::_send_sftp_packet()
public _get_sftp_packet ( ) : string
return string

_get_stat_cache_prop() public method

Uses cache if appropriate.
public _get_stat_cache_prop ( string $path, string $prop ) : mixed
$path string
$prop string
return mixed

_get_xstat_cache_prop() public method

Uses cache if appropriate.
public _get_xstat_cache_prop ( string $path, string $prop, $type ) : mixed
$path string
$prop string
return mixed

_list() public method

Reads a list, be it detailed or not, of files in the given directory
public _list ( string $dir, boolean $raw = true ) : mixed
$dir string
$raw boolean
return mixed

_logError() public method

Logs errors
public _logError ( string $response, integer $status )
$response string
$status integer

_mkdir_helper() public method

Helper function for directory creation
public _mkdir_helper ( string $dir, $attr ) : boolean
$dir string
return boolean

_nlist_helper() public method

Helper method for nlist
public _nlist_helper ( string $dir, boolean $recursive, string $relativeDir ) : mixed
$dir string
$recursive boolean
$relativeDir string
return mixed

_parseAttributes() public method

See '7. File Attributes' of draft-ietf-secsh-filexfer-13 for more info.
public _parseAttributes ( string &$response ) : array
$response string
return array

_parseLongname() public method

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 SSH_FXP_NAME 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.
public _parseLongname ( string $longname ) : mixed
$longname string
return mixed

_parseMode() public method

Quoting the SFTP RFC, "Implementations MUST NOT send bits that are not defined" but they seem to anyway
public _parseMode ( integer $mode ) : integer
$mode integer
return integer

_query_stat_cache() public method

Mainly used by file_exists
public _query_stat_cache ( $path ) : mixed
return mixed

_read_put_responses() public method

Sending an SSH_FXP_WRITE packet and immediately reading its response isn't as efficient as blindly sending out $i SSH_FXP_WRITEs, in succession, and then reading $i responses.
public _read_put_responses ( integer $i ) : boolean
$i integer
return boolean

_realpath() public method

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.
See also: self::chdir()
public _realpath ( string $path ) : mixed
$path string
return mixed

_remove_from_stat_cache() public method

Remove files / directories from cache
public _remove_from_stat_cache ( string $path ) : boolean
$path string
return boolean

_send_sftp_packet() public method

See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info.
See also: self::_get_sftp_packet()
See also: self::_send_channel_packet()
public _send_sftp_packet ( integer $type, string $data ) : boolean
$type integer
$data string
return boolean

_setstat() public method

Sets information about a file
public _setstat ( string $filename, string $attr, boolean $recursive ) : boolean
$filename string
$attr string
$recursive boolean
return boolean

_setstat_recursive() public method

Minimizes directory lookups and SSH_FXP_STATUS requests for speed.
public _setstat_recursive ( string $path, string $attr, integer &$i ) : boolean
$path string
$attr string
$i integer
return boolean

_stat() public method

Determines information without calling \phpseclib\Net\SFTP::_realpath(). The second parameter can be either NET_SFTP_STAT or NET_SFTP_LSTAT.
public _stat ( string $filename, integer $type ) : mixed
$filename string
$type integer
return mixed

_update_stat_cache() public method

Save files / directories to cache
public _update_stat_cache ( string $path, mixed $value )
$path string
$value mixed

chdir() public method

Changes the current directory
public chdir ( string $dir ) : boolean
$dir string
return boolean

chgrp() public method

Returns true on success or false on error.
public chgrp ( string $filename, integer $gid, boolean $recursive = false ) : boolean
$filename string
$gid integer
$recursive boolean
return boolean

chmod() public method

Returns the new file permissions on success or false on error. If $recursive is true than this just returns true or false.
public chmod ( integer $mode, string $filename, boolean $recursive = false ) : mixed
$mode integer
$filename string
$recursive boolean
return mixed

chown() public method

Returns true on success or false on error.
public chown ( string $filename, integer $uid, boolean $recursive = false ) : boolean
$filename string
$uid integer
$recursive boolean
return boolean

clearStatCache() public method

Clear the stat cache
public clearStatCache ( )

delete() public method

Deletes a file on the SFTP server.
public delete ( string $path, boolean $recursive = true ) : boolean
$path string
$recursive boolean
return boolean

disableStatCache() public method

Disable the stat cache
public disableStatCache ( )

enableStatCache() public method

Enable the stat cache
public enableStatCache ( )

file_exists() public method

Checks whether a file or directory exists
public file_exists ( string $path ) : boolean
$path string
return boolean

fileatime() public method

Gets last access time of file
public fileatime ( string $path ) : mixed
$path string
return mixed

filegroup() public method

Gets file group
public filegroup ( string $path ) : mixed
$path string
return mixed

filemtime() public method

Gets file modification time
public filemtime ( string $path ) : mixed
$path string
return mixed

fileowner() public method

Gets file owner
public fileowner ( string $path ) : mixed
$path string
return mixed

fileperms() public method

Gets file permissions
public fileperms ( string $path ) : mixed
$path string
return mixed

filesize() public method

Gets file size
public filesize ( string $path ) : mixed
$path string
return mixed

filetype() public method

Gets file type
public filetype ( string $path ) : mixed
$path string
return mixed

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

getLastSFTPError() public method

Returns the last error
public getLastSFTPError ( ) : string
return string

getSFTPErrors() public method

Returns all errors
public getSFTPErrors ( ) : string
return string

getSFTPLog() public method

Returns a string if NET_SFTP_LOGGING == self::LOG_COMPLEX, an array if NET_SFTP_LOGGING == self::LOG_SIMPLE and false if !defined('NET_SFTP_LOGGING')
public getSFTPLog ( ) : string
return string or Array

getSupportedVersions() public method

Get supported SFTP versions
public getSupportedVersions ( ) : array
return array

is_dir() public method

Tells whether the filename is a directory
public is_dir ( string $path ) : boolean
$path string
return boolean

is_file() public method

Tells whether the filename is a regular file
public is_file ( string $path ) : boolean
$path string
return boolean

is_readable() public method

Tells whether a file exists and is readable
public is_readable ( string $path ) : boolean
$path string
return boolean

is_writable() public method

Tells whether the filename is writable
public is_writable ( string $path ) : boolean
$path string
return boolean

is_writeable() public method

Alias of is_writable
public is_writeable ( string $path ) : boolean
$path string
return boolean

login() public method

Login
public login ( string $username ) : boolean
$username string
return boolean

lstat() public method

Returns an array on success and false otherwise.
public lstat ( string $filename ) : mixed
$filename string
return mixed

mkdir() public method

Creates a directory.
public mkdir ( string $dir, $mode, $recursive = false ) : boolean
$dir string
return boolean

nlist() public method

Returns a list of files in the given directory
public nlist ( string $dir = '.', boolean $recursive = false ) : mixed
$dir string
$recursive boolean
return mixed

put() public method

By default, \phpseclib\Net\SFTP::put() does not read from the local filesystem. $data is dumped directly into $remote_file. So, for example, if you set $data to 'filename.ext' and then do \phpseclib\Net\SFTP::get(), you will get a file, twelve bytes long, containing 'filename.ext' as its contents. Setting $mode to self::SOURCE_LOCAL_FILE will change the above behavior. With self::SOURCE_LOCAL_FILE, $remote_file will contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how large $remote_file will be, as well. Setting $mode to self::SOURCE_CALLBACK 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 If $data is a resource then it'll be used as a resource instead. 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 - self::RESUME and self::RESUME_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: self::SOURCE_LOCAL_FILE | self::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 self::RESUME with self::RESUME_START. If $mode & (self::RESUME | self::RESUME_START) then self::RESUME_START will be assumed. $start and $local_start give you more fine grained control over this process and take precident over self::RESUME when they're non-negative. ie. $start could let you write at the end of a file (like self::RESUME) or in the middle of one. $local_start could let you start your reading from the end of a file (like self::RESUME_START) or in the middle of one. Setting $local_start to > 0 or $mode | self::RESUME_START doesn't do anything unless $mode | self::SOURCE_LOCAL_FILE.
public put ( string $remote_file, string | resource $data, integer $mode = self::SOURCE_STRING, integer $start, integer $local_start, callable | null $progressCallback = null ) : boolean
$remote_file string
$data string | resource
$mode integer
$start integer
$local_start integer
$progressCallback callable | null
return boolean

pwd() public method

Returns the current directory name
public pwd ( ) : mixed
return mixed

rawlist() public method

Returns a detailed list of files in the given directory
public rawlist ( string $dir = '.', boolean $recursive = false ) : mixed
$dir string
$recursive boolean
return mixed

realpath() public method

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

rename() public method

Renames a file or a directory on the SFTP server
public rename ( string $oldname, string $newname ) : boolean
$oldname string
$newname string
return boolean

rmdir() public method

Removes a directory.
public rmdir ( string $dir ) : boolean
$dir string
return boolean

setListOrder() public method

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', SORT_ASC); $sftp->setListOrder('size', SORT_DESC, '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
public setListOrder ( )

size() public method

Files larger than 4GB will show up as being exactly 4GB.
public size ( string $filename ) : mixed
$filename string
return mixed

stat() public method

Returns an array on success and false otherwise.
public stat ( string $filename ) : mixed
$filename string
return mixed

touch() public method

If the file does not exist, it will be created.
public touch ( string $filename, integer $time = null, integer $atime = null ) : boolean
$filename string
$time integer
$atime integer
return boolean

truncate() public method

Truncates a file to a given length
public truncate ( string $filename, integer $new_size ) : boolean
$filename string
$new_size integer
return boolean

Property Details

$extensions public_oe property

Extensions supported by the server
See also: self::_initChannel()
public array $extensions
return array

$max_sftp_packet public_oe property

Max SFTP Packet Size
See also: self::__construct()
See also: self::get()
public array $max_sftp_packet
return array

$packet_buffer public_oe property

Packet Buffer
See also: self::_get_sftp_packet()
public string $packet_buffer
return string

$packet_log public_oe property

Packet Log
See also: self::getLog()
public array $packet_log
return array

$packet_type public_oe property

The request ID exists in the off chance that a packet is sent out-of-order. Of course, this library doesn't support concurrent actions, so it's somewhat academic, here.
See also: self::_get_sftp_packet()
public int $packet_type
return integer

$packet_type_log public_oe property

Packet Type Log
See also: self::getLog()
public array $packet_type_log
return array

$packet_types public_oe property

Packet Types
See also: self::__construct()
public array $packet_types
return array

$pwd public_oe property

Current working directory
See also: self::_realpath()
See also: self::chdir()
public string $pwd
return string

$request_id public_oe property

The request ID exists in the off chance that a packet is sent out-of-order. Of course, this library doesn't support concurrent actions, so it's somewhat academic, here.
See also: self::_send_sftp_packet()
public int $request_id
return integer

$sftp_errors public_oe property

Error information
See also: self::getSFTPErrors()
See also: self::getLastSFTPError()
public string $sftp_errors
return string

$sortOptions public_oe property

Sort Options
See also: self::_comparator()
See also: self::setListOrder()
public array $sortOptions
return array

$stat_cache public_oe property

Rather than always having to open a directory and close it immediately there after to see if a file is a directory we'll cache the results.
See also: self::_update_stat_cache()
See also: self::_remove_from_stat_cache()
See also: self::_query_stat_cache()
public array $stat_cache
return array

$status_codes public_oe property

Status Codes
See also: self::__construct()
public array $status_codes
return array

$use_stat_cache public_oe property

Stat Cache Flag
See also: self::disableStatCache()
See also: self::enableStatCache()
public bool $use_stat_cache
return boolean

$version public_oe property

Server SFTP version
See also: self::_initChannel()
public int $version
return integer