ほとんどないけど、SFTP接続して他サーバへファイルをアップしたいときってあると思う。
入稿用データだとかそういうのとか。
ってことで今日はそのお話をば。

$server = "xxx.xxx.xxx.xxx";
$port = "22";
$username = "user";
$password = "password";
$connect = ssh2_connect($server,$port);
if(ssh2_auth_password($connect,$username,$password)){
$sftp = ssh2_sftp($connect);
$stream = fopen("ssh2.sftp://{$sftp}/foo/bar/hoge.txt.tmp",'w');
$file = file_get_contents("/var/www/html/local/hoge.txt");
fwrite($stream, $file);
fclose($stream);
ssh2_sftp_rename($sftp,"/foo/bar/hoge.txt.tmp","/foo/bar/hoge.txt");
ssh2_exec($connect,"exit");
}
こんな感じ。fopen使ってSFTP接続先に書き込んじゃうみたいな。
実際はなんか下記のように色々と便利な関数があるっぽいんだけど、自分の環境では使えなかった。
// linux commandを使う ssh2_exec($connect,"linux command"); // scp経由でファイルを送信する ssh2_scp_send($connect,'/local/filename','/remote/filename',0644);SSH2関数一覧
とりあえず環境によってSSH関数がうまく使えないとかあるかもしれないので、
fopen()使ったりとかしてうまくやればなんとかなるんじゃないかと。
外部とシステム的に連携しようとかそういう場合だとやっぱりこういう感じになるんじゃないかな的な。

For creating a connection with SFTP in PHP, PHPseclib is a great library. It is real easy to install and use. You can upload and download the files from ftp with this. However, you will need to have ssh access.
返信削除ssh2_scp_sendはscpプロトコルのため、サーバー側に設定がなかったのではないでしょうか?
返信削除sftpの設定のみであれば、できないはずです。(詳しいことは自分もわかりませんが、yum updateでsftpは使えるようになるようです。)