1、用phpinfo();查看php版本信息,通过下面截图可以看出php版本是php7.3.4,TS,VC15,php.ini目录C:\soft\php-7.3.4\php.ini
2、下载对应版本的扩展文件,下载地址:https://windows.php.net/downloads/pecl/snaps/ssh2/1.2-dev/
php_ssh2-1.2-dev-7.3-ts-vc15-x64.zip
3、解压后台把 php_ssh2.dll 文件拷贝到 C:\soft\php-7.3.4\ext下,然后修改php.ini文件 extension=ssh2,然后重启apache
extension=ssh2
;extension=bz2
extension=curl
extension=fileinfo
extension=gd2
4、刷新页面,查看phpinfo();信息中是否有ssh2
1、安装相应文件(php7.3需要下git上最新源码才可以安装)
yum install -y libssh2 libssh2-devel git
git clone https://git.php.net/repository/pecl/networking/ssh2.git
cd ssh2
phpize
./configure --with-php-config=/usr/local/php/bin/php-config --prefix=/vol/usr/local/lamp/ssh2 --with-ssh2=/vol/usr/local/lamp/libssh2
make
make install
echo "extension=ssh2.so">>/usr/local/php/lib/php.ini
systemctl restart php-fpm
1、SFTP类
"", //SFTP服务器ip地址
"username" => "", //SFTP服务器用户名
"password" => "", //SFTP服务器密码(有密码就不用提供公钥,秘钥)
"port" => "22", //SFTP服务器端口号
"pubkeyfile" => "id_rsa_logo.pub", //SFTP服务器秘钥文件
"privkeyfile" => "id_rsa_logo", //SFTP服务器公钥文件
"passphrase" => ""
); //secret
$methods['hostkey'] = $this->usePubKey ? 'ssh-rsa' : [] ;
$this->conn = ssh2_connect($sftp_config['host'], $sftp_config['port'], $methods);
if($this->usePubKey){
// 使用秘钥登录
ssh2_auth_pubkey_file($this->conn,$sftp_config['username'],$sftp_config['pubkeyfile'],$sftp_config['privkeyfile'],$sftp_config['passphrase']);
$this->resSftp = ssh2_sftp($this->conn);
}else{
// 使用用户名和密码登录
ssh2_auth_password( $this->conn, $sftp_config['username'],$sftp_config['password']);
$this->resSftp = ssh2_sftp($this->conn);
}
}
// 下载文件
public function download($remote, $local){
return copy("ssh2.sftp://{$this->resSftp}".$remote, $local);
}
// 文件上传
public function upload($remote, $local) //, $file_mode = 0777
{
return copy($remote,"ssh2.sftp://{$this->resSftp}".$local);
}
// 创建目录
public function mkdir($path) //使用创建目录循环
{
ssh2_sftp_mkdir($this->resSftp, $path,0777,true);
}
// 判段目录或者文件是否存在
public function exits($dir){
return file_exists("ssh2.sftp://{$this->resSftp}".$dir);
}
2、使用
exits($serverPath);
//如果目录不存在,创建目录
if (!$re) {
$this->mkdir($serverPath);
}
//上传到sftp
$this->upload($files.'test.txt', $serverPath.'test.txt');
//下载文件
//判断远程文件是否存在
$re = $this->exits($serverPath.'test.txt');
//如果不存在
if (!$re) {
echo "文件不存在无法下载";
die;
}
$this->download($serverPath.'test.txt', $files.'test.txt');