1. 程式人生 > >php 通過ssh2協議用sftp實現檔案上傳、下載

php 通過ssh2協議用sftp實現檔案上傳、下載

伺服器的連線

$connection=ssh2_connect($host, $port);
if( ssh2_auth_pubkey_file($connection, $send_account,
        $file.'/id_dsa.pub',
        $file.'/id_dsa')) {
        echo "Public Key Authentication Successful\n";

}

//初始化 SFTP

$resSFTP = ssh2_sftp($connection);

//下載檔案

 $filename = APPLICATION_PATH . '/../data/edi/upload/1'
.time().'.txt'; $opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>60, ) ); $context = stream_context_create($opts); $strData = file_get_contents("ssh2.sftp://{$resSFTP}//downloadAmazonConnectivityTest", false, $context); file_put_contents($filename, $strData);

//上傳檔案

$re = copy(APPLICATION_PATH . '/../data/edi/YourConnectivityTest'
, "ssh2.sftp://{$resSFTP}/upload/1.txt" );