簡體   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