phpseclib: SFTP Examples and Notes

Login:

Action:

<?php
include('Net/SFTP.php');
include('Crypt/RSA.php');

define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}$key = new Crypt_RSA();
$key->loadKey(file_get_contents('privatekey'));
if (!$sftp->login('username', $key)) {
    exit('Login Failed');
}$key = new Crypt_RSA();
$key->setPassword('whatever');
$key->loadKey(file_get_contents('privatekey'));
if (!$sftp->login('username', $key)) {
    exit('Login Failed');
}

// puts a three-byte file named filename.remote on the SFTP server
$sftp->put('filename.remote', 'xxx');// copies filename.local to filename.remote on the SFTP server
$sftp->put('filename.remote', 'filename.local', NET_SFTP_LOCAL_FILE);// outputs the contents of filename.remote to the screen
echo $sftp->get('filename.remote');// copies filename.remote to filename.local from the SFTP server
$sftp->get('filename.remote', 'filename.local');$sftp->mkdir('test'); // create directory 'test'
$sftp->chdir('test'); // open directory 'test'
echo $sftp->pwd(); // show that we're in the 'test' directory
$sftp->chdir('..'); // go back to the parent directory
$sftp->rmdir('test'); // delete the directory
// if the directory had files in it we'd need to do a recursive delete
//$sftp->delete('test', true);
print_r($sftp->nlist()); // == $sftp->nlist('.')
print_r($sftp->rawlist()); // == $sftp->rawlist('.')
$sftp->chmod(0777, 'filename.remote');
//$sftp->chmod(0777, 'dirname.remote', true); // recursively change permissions on a directory
echo $sftp->size('filename.remote');
print_r($sftp->stat('filename.remote'));
print_r($sftp->lstat('filename.remote'));$sftp->delete('filename.remote'); // doesn't delete directories
// recursive delete
$sftp->delete('dirname.remote', true); // deletes a directory and all its contents
$sftp->rename('filename.remote', 'newname.remote');

echo $ssh->getSFTPLog();
?>

Output of nlist() and rawlist()

$sftp->nlist():
Array
(
    [0] => uploads
    [1] => ..
    [2] => .
    [3] => .profile
    [4] => .bashrc
    [5] => .bash_logout
)
$sftp->rawlist():
Array
(
    [uploads] => Array
        (
            [size] => 4096
            [uid] => 1001
            [gid] => 1002
            [permissions] => 16877
            [atime] => 1338498490
            [mtime] => 1338497853
            [type] => 2
        )

    [..] => Array
        (
            [size] => 4096
            [uid] => 0
            [gid] => 0
            [permissions] => 16877
            [atime] => 1338499576
            [mtime] => 1338497853
            [type] => 2
        )

    [.] => Array
        (
            [size] => 4096
            [uid] => 0
            [gid] => 0
            [permissions] => 16877
            [atime] => 1338499576
            [mtime] => 1338497853
            [type] => 2
        )

    [.profile] => Array
        (
            [size] => 675
            [uid] => 1012
            [gid] => 1013
            [permissions] => 33188
            [atime] => 1338497357
            [mtime] => 1338497357
            [type] => 1
        )

    [.bashrc] => Array
        (
            [size] => 3353
            [uid] => 1012
            [gid] => 1013
            [permissions] => 33188
            [atime] => 1338497357
            [mtime] => 1338497357
            [type] => 1
        )

    [.bash_logout] => Array
        (
            [size] => 220
            [uid] => 1012
            [gid] => 1013
            [permissions] => 33188
            [atime] => 1338497357
            [mtime] => 1338497357
            [type] => 1
        )

)
The type index corresponds to one of the following named constants:
  • NET_SFTP_TYPE_REGULAR
  • NET_SFTP_TYPE_DIRECTORY
  • NET_SFTP_TYPE_SYMLINK
  • NET_SFTP_TYPE_SPECIAL

stat() vs. lstat() vs. size()

stat() and lstat() return associative arrays with misc information about the files. lstat() and stat() are identical with the caveat that when the file in question is a symbolic link the information returned refers to the link itself and not the file (or directory) being linked to.

size() returns the 'size' index of the associative array returned by lstat()