phpseclib: SFTP Examples and Notes

Action:

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

define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}

// puts a three-byte file named filename.remote on the SFTP server
$sftp->put('filename.remote', 'xxx');
// puts an x-byte file named filename.remote on the SFTP server,
// where x is the size of filename.local
$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');
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
// has the same syntax as http://php.net/touch
$sftp->touch('filename.remote');
$sftp->chown('filename.remote', $uid);
//$sftp->chown('filename.remote', $uid, true); // recursively change the owner
$sftp->chgrp('filename.remote', $gid);
//$sftp->chgrp('filename.remote', $gid, true); // recursively change the group
$sftp->truncate('filename.remote', $size);
echo $sftp->size('filename.remote');
print_r($sftp->stat('filename.remote'));
print_r($sftp->lstat('filename.remote'));$sftp->delete('filename.remote'); // deletes directories recursively
// non-recursive delete
$sftp->delete('dirname.remote', false);
$sftp->rename('filename.remote', 'newname.remote');

echo $sftp->getSFTPLog();
?>

Output of nlist() and rawlist()

$sftp->nlist():
  • 0
    • uploads
  • 1
    • ..
  • 2
    • .
  • 3
    • .profile
  • 4
    • .bashrc
  • 5
    • .bash_logout
$sftp->rawlist():
  • uploads
    • size
      • 4096
    • uid
      • 1001
    • gid
      • 1002
    • permissions
      • 16877
    • atime
      • 1338498490
    • mtime
      • 1338497853
    • type
      • 2
  • ..
    • size
      • 4096
    • uid
      • 0
    • gid
      • 0
    • permissions
      • 16877
    • atime
      • 1338499576
    • mtime
      • 1338497853
    • type
      • 2
  • .
    • size
      • 4096
    • uid
      • 0
    • gid
      • 0
    • permissions
      • 16877
    • atime
      • 1338499576
    • mtime
      • 1338497853
    • type
      • 2
  • .profile
    • size
      • 675
    • uid
      • 1012
    • gid
      • 1013
    • permissions
      • 33188
    • atime
      • 1338497357
    • mtime
      • 1338497357
    • type
      • 1
  • bashrc
    • size
      • 3353
    • uid
      • 1012
    • gid
      • 1013
    • permissions
      • 33188
    • atime
      • 1338497357
    • mtime
      • 1338497357
    • type
      • 1
  • .bash_lougout
    • size
      • 270
    • 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()

Uploading files

The function definition for put() is as follows:

function put($remote_file, $data, $mode = NET_SFTP_STRING, $start = -1, $local_start = -1)

Uploading strings vs. files

$sftp->put('filename.remote', 'filename.local') creates filename.remote on the remote server with 'filename.local' as the contents.

$sftp->put('filename.remote', 'xxx', NET_SFTP_LOCAL_FILE) creates filename.remote on the remote server such that the contents of it and filename.local match. ie. with NET_SFTP_LOCAL_FILE it uploads a file and without it it uploads a string.

Resuming transfers

$sftp->put('filename.remote', 'xxx', NET_SFTP_RESUME) will append 'xxx' to filename.remote.

$sftp->put('filename.remote', 'filename.local', NET_SFTP_LOCAL_FILE | NET_SFTP_RESUME_START) will append filename.remote to filename.local.

$sftp->put('filename.remote', 'filename.local', NET_SFTP_LOCAL_FILE | NET_SFTP_RESUME) will append all but the first $sftp->size('filename.remote') bytes of filename.local to filename.remote. The idea being that if your transfer is interupted you can restart it.

Positional control

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

Downloading files

The function definition for get() is as follows:

function get($remote_file, $local_file = false, $offset = 0, $length = -1)

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.