php 如何使用ssh2列出其他服务器中目录的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8840883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to list files of a directory in an other server using ssh2
提问by pcs
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 able to fetch a desired file but I am not sure how the files can be listed.
我使用 ssh2_connect 函数连接到另一台服务器,连接进行得很顺利,我能够获取所需的文件,但我不确定如何列出这些文件。
回答by Elias Dorneles
You can use ssh2_sftpand 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";
}
回答by pcs
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:
如果有人正在努力使其工作,并且您正在运行,PHP 5.6.28
那么最近的更新要么创建了一个要求,要么引入了一个intval()
必须在每个 SFTP 文件夹/文件访问功能上使用的错误:
$handle = opendir("ssh2.sftp://".intval($sftp)."/path/to/directory");
回答by Uku Loskit
http://www.php.net/manual/en/function.ssh2-exec.php
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.
你给它ls
命令,假设它是一个基于 UNIX 的系统(通常是这种情况),否则像dir
Windows的特定于 OP 的命令。
<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, 'ls');
?>
回答by Pan
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;
}
}