简体   繁体   English

列出远程主机上的文件失败

[英]Listing files on remote host failed

I have a code: 我有一个代码:

scandir("ssh2.sftp://" . intval($sftpHandle) . $remoteDir);

Why the same code works for one server but doesn't for another? 为什么相同的代码可用于一台服务器,但不适用于另一台? There is a files on both servers. 两台服务器上都有一个文件。 I can manage them via Filezilla without problems. 我可以通过Filezilla对其进行管理,而不会出现问题。 The first one just returns array('.') even if there is a lot of files, another one returns array('file1', 'file2', 'file3', etc) 即使有很多文件,第一个也会返回array('.') ,另一个会返回array('file1', 'file2', 'file3', etc)

Even if I cannot list a files using scandir(), command ssh2_scp_recv($sshHandle, $remoteDir/file1, $localDir/file1) works fine. 即使我无法使用scandir()列出文件,命令ssh2_scp_recv($sshHandle, $remoteDir/file1, $localDir/file1)可以正常工作。 Also ssh2_exec($sshHandle, "ls $remoteDir") works fine to me. ssh2_exec($sshHandle, "ls $remoteDir")对我也很好。

Please note that I'm using $ ftpHandle for scandir() but $ sshHandle for ssh2_* functions. 请注意,我用$ ftpHandle为SCANDIR(),但$ sshHandle为ssh2_ *功能。 Using $sshHandle for scandir() cause "Segmentation fault" error. 将$ sshHandle用于scandir()会导致“分段错误”错误。

I know that I can workaround this by parsing ssh2_exec($sshHandle, "ls $remoteDir") output, but would prefer do it right way if possible. 我知道我可以通过解析ssh2_exec($sshHandle, "ls $remoteDir")输出来解决此问题,但是如果可能的话,我更愿意这样做。

My PHP version is 7.0.31 我的PHP版本是7.0.31

This should work probably 这应该可行

$connection = ssh2_connect($url);

// login
if (!ssh2_auth_password($connection, $username, $password)) throw new Exception('Unable to connect server.');

// Create SFTP resource
if (!$sftp = ssh2_sftp($connection)) throw new Exception('Unable to create connection.');

$localDir  = '/path/to/local/dir';
$remoteDir = '/path/to/remote/dir';

// download all the files or list all
$files    = scandir('ssh2.sftp://' . $sftp . $remoteDir);

if (!empty($files)) {
  foreach ($files as $file) {
    if ($file != '.' && $file != '..') {


       // here you can print files using $file

       // or download file by uncommenting below line

      //ssh2_scp_recv($connection, "$remoteDir/$file", "$localDir/$file");

    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM