php使用sftp需要ssh2扩展,我们在下载扩展的时候,我们需要看我们安装php的版本。我的版本是PHP Version 7.2.12,所以我下载的版本是https://windows.php.net/downloads/pecl/releases/ssh2/1.1.2/php_ssh2-1.1.2-7.2-ts-vc15-x86.zip
下载好了之后我们解压之后
我们将将 php_ssh.dll、php_ssh2.pdb 放到你的 php 扩展目录下 php/ext/ 下。
在php.ini中加入 extension=php_ssh2.dll
配置好了之后,在浏览器中可以看到
下面试简单的测试代码
public function c_ftp(){
$host = "xxxxxxx";
$port = "65535";
$user = "root";
$pwd = "xxxxxxxx";
$conn = ssh2_connect($host,$port);
$res = ssh2_auth_password($conn,$user,$pwd);
$ressftp = ssh2_sftp($conn);
$local = "C:\ceshi\index.php";
$remote = "/root/index.php";
// $r = copy($local,"ssh2.sftp://{$ressftp}".$remote); 上传
$r=copy("ssh2.sftp://{$ressftp}".$remote, $local); //下载
if ($r){
echo "成功";
}else{
echo "失败";
}
}