phpseclibを利用
use phpseclib\Net\SFTP; use physiclib\Crypt\RSA;
接続仕様
$ssh_host = ホスト名 or IPアドレス $ssh_port = ポート番号 $ssh_user_id = ユーザーID $ssh_password = パスワード認証の場合 パスワード $rsa_file_path = 鍵認証の場合 プライベート鍵ファイルへのパス $csv_share_directory = 送信先ディレクトリ
実行
$sftp = new SFTP($ssh_host, $ssh_port);
// password or rsa.
$key = $ssh_password;
if (!$key) {
$key = new RSA();
$rsa = file_get_contents($rsa_file_path);
if (!$rsa) {
$this->putError(t('rsaファイルが読み込めませんでした。@file', ['@file' => $rsa_file_path]));
}
if (empty($this->getError())) {
$key->loadKey($rsa);
}
}
// login.
if (empty($this->getError())) {
if ($sftp->login($ssh_user_id, $key)) {
// put file.
$sftp->put("$csv_share_directory/$file", $data);
}
else {
$this->putError(t('SSHログインに失敗しました。'));
}
}