リモートサーバーからファイルのみを移動し、ファイルディレクトリをデータベースに配置しようとしているため、ファイルとディレクトリを区別できる必要があります。SSH2 経由で正常に接続でき、リモート パスのトップ ディレクトリ内のファイルとディレクトリを読み取って表示できます。しかし、返された名前がディレクトリなのかファイルなのかを確認できる php スクリプトを見つけることができませんでした。以下は、私たちが試したいくつかの例です。どんな助けでも大歓迎です。事前に感謝します。
$connection = ssh2_connect('www.myremote.com', 22);
ssh2_auth_password($connection, 'user', 'pw');
$sftp = ssh2_sftp($connection);
// THIS WORKS NICELY TO DISPLAY NAME FROM REMOTE SERVER
$handle = opendir("ssh2.sftp://$sftp/remotepath/");
echo "Directory handle: $handle<br>";
echo "Entries:<br>";
while (false != ($entry = readdir($handle))){
echo "$entry<br>";
}
// ONE OPTION THAT DOES NOT WORK
$handle = opendir("ssh2.sftp://$sftp/remotepath/");
echo "<br><br>2Directory handle: $handle<br>";
echo "4Entries:<br>";
while (false != ($entry = readdir($handle))){
if (is_dir(readdir($handel.''.$entry) ) ) {echo "<strong>$entry</strong><br>"; } else {
echo "$entry<br>"; //}
}
// ANOTHER OPTION THAT RETURNS AN EMPTY RESULT
$files = scandir('ssh2.sftp://'.$sftp.'/remotepath/');
foreach ($files as $file):
if (is_dir('ssh2.sftp://'.$sftp.'/remotepath/'.$file) ) {"<strong>".$file."</strong><br>"; } else { $file.'<br>'; }
endforeach;