windows/linux 下php7.3安装php_ssh2扩展和使用ssh链接sftp上传下载文件

23 篇文章 1 订阅

一、windows php7.3 php_ssh2扩展安装

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


 二、linux centos下php7.3 php_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

 

三、使用ssh连接sftp上传下载文件

1、SFTP类

<?php
/*
* SFTP
* User: jason
* Date: 2019/5/29
* Time: 14:35
* @class Sftp 
*/

class Sftp {

    // 连接为NULL
    private $conn = NULL;

    // 是否使用秘钥登陆
    private $usePubKey= true;

    //sftp resource
    private $resSftp = NULL;

    /**
     *  构造函数.
     */
    public function __construct() {
     
        $sftp_config = array(
            "host" => "",     //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、使用

<?php
$this = new Sftp();
$files =  __DIR__."/test/";  //本地文件路径;
$serverPath = "/test/";     //远程路径

//上传文件
//判断远程路径是否存在
$re = $this->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');


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值