繁体   English   中英

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

[英]Listing files on remote host failed

我有一个代码:

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

为什么相同的代码可用于一台服务器,但不适用于另一台? 两台服务器上都有一个文件。 我可以通过Filezilla对其进行管理,而不会出现问题。 即使有很多文件,第一个也会返回array('.') ,另一个会返回array('file1', 'file2', 'file3', etc)

即使我无法使用scandir()列出文件,命令ssh2_scp_recv($sshHandle, $remoteDir/file1, $localDir/file1)可以正常工作。 ssh2_exec($sshHandle, "ls $remoteDir")对我也很好。

请注意,我用$ ftpHandle为SCANDIR(),但$ sshHandle为ssh2_ *功能。 将$ sshHandle用于scandir()会导致“分段错误”错误。

我知道我可以通过解析ssh2_exec($sshHandle, "ls $remoteDir")输出来解决此问题,但是如果可能的话,我更愿意这样做。

我的PHP版本是7.0.31

这应该可行

$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