先上代码吧
<?php
require_once ("vendor/autoload.php");
use phpseclib\Crypt\RSA;
use phpseclib\Net\SFTP;
$sftp = new SFTP('Ip|Adress');
// create new RSA key
$privateKey = new RSA();
// in case that key has a password
//$privateKey->setPassword('private key password');
// load the private key
$privateKey->loadKey(file_get_contents('/Users/path/to/auth.pem'));
// login via sftp
if (!$sftp->login('root', $privateKey)) {
throw new Exception('sFTP login failed');
}
// now you can list what's in here
//$filesAndFolders = $sftp->nlist();
// you can change directory
//$sftp->chdir('coolstuffdir');
// get a file
//$sftp->get('remoteFile', 'localFile');
// create a remote new file with defined content
//$sftp->put('newfile.txt', 'new file content');
// put a local file 第一个参数是远程地址,第二个参数是本地地址,第三个是固定常量
$sftp->put('/data/remote/dir/image.png', '/Users/path/to/image.png', $sftp::SOURCE_LOCAL_FILE);
`
composer.json文件内容,注意版本,因为v3和v2有一些区别,这里使用到了v2版本
{
"require": {
"phpseclib/phpseclib": "^2.0"
}
}
暂无评论
要发表评论,您必须先 登录