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

后端 未结 4 1348
北海茫月
北海茫月 2020-12-16 13:49

I would like to list the files of a directory in an other server

I am connected to an other server using ssh2_connect function the connection is going well and I am

相关标签:
4条回答
  • 2020-12-16 14:41

    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');
    ?>
    
    0 讨论(0)
  • 2020-12-16 14:42

    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");
    
    0 讨论(0)
  • 2020-12-16 14:45

    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;
      }
    }
    
    0 讨论(0)
  • 2020-12-16 14:50

    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";
    }
    
    0 讨论(0)
提交回复
热议问题