How to list files of a directory in an other server using ssh2

Here's a method which can scan directories recursively and return multi-dimensional arrays if the recursive parameter is set, or only scan the path and return a single dimension array containing files in that directory if it's not. Can be modified to also include directories without contents in non-recursive mode if needed.

Creating it as a class makes it easy to be reused later. I only included the methods from my class that were required to answer the question.

$host      = 'example.com';
$port      = 22;
$username  = 'user1';
$password  = 'password123';
$path      = '.';
$recursive = true;

$conn = new SFTP($host, $port);
$conn->login($username, $password);
$files = $conn->ls($path, $recursive);
var_dump($files);

class SFTP
{
  private $connection;
  private $sftp;

  public function __construct($host, $port = 22)
  {
    $this->connection = @ssh2_connect($host, $port);
    if (! $this->connection)
      throw new Exception("Could not connect to $host on port $port.");
  }

  public function login($username, $password)
  {
    if (! @ssh2_auth_password($this->connection, $username, $password))
      throw new Exception("Could not authenticate with username $username");
    $this->sftp = @ssh2_sftp($this->connection);
    if (! $this->sftp)
      throw new Exception("Could not initialize SFTP subsystem.");
  }

  public function ls($remote_path, $recursive = false)
  {
    $tmp      = $this->sftp;
    $sftp     = intval($tmp);
    $dir      = "ssh2.sftp://$sftp/$remote_path";
    $contents = array();
    $handle   = opendir($dir);

    while (($file = readdir($handle)) !== false) {
      if (substr("$file", 0, 1) != "."){
        if (is_dir("$dir/$file")){
          if ($recursive) {
            $contents[$file] = array();
            $contents[$file] = $this->ls("$remote_path/$file", $recursive);
          }
        } else {
          $contents[] = $file;
        }
      }
    }

    closedir($handle);
    return $contents;
  }
}

http://www.php.net/manual/en/function.ssh2-exec.php

You give it the ls command, assuming it is a UNIX-based system (usually the case), otherwise the OP-specific command like dir for Windows.

<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$stream = ssh2_exec($connection, 'ls');
?>

You can use ssh2_sftp and opendir, like this:

<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$sftp = ssh2_sftp($connection);
$sftp_fd = intval($sftp);

$handle = opendir("ssh2.sftp://$sftp_fd/path/to/directory");
echo "Directory handle: $handle\n";
echo "Entries:\n";
while (false != ($entry = readdir($handle))){
    echo "$entry\n";
}

In case anybody is struggling to get this to work, and you are running PHP 5.6.28 there was a recent update that either created a requirement or introduced a bug where intval() must be used on each SFTP folder/file access function:

$handle = opendir("ssh2.sftp://".intval($sftp)."/path/to/directory");

Tags:

Php

Ssh