php使用sftp

2020-09-05

使用sftp需要安装ssh2扩展

windows下安装

下载地址:https://pecl.php.net/package/ssh2

下载对应版本的dll
php_ssh2.dll

配置php.ini
extension=php_ssh2.dll

linux下安装ssh2扩展:

安装依赖libssh2
yum install libssh2 libssh2-devel -y

pecl 安装ssh2(如果提示pecl命令不存在,那么先安装相应php版本的pecl)
pecl install -f ssh2-1.1.2 (-1.1.2表示版本,不指定则安装默认版本,如果版本与php版本不对应,可能无法安装)

注:ssh2需要对应php版本:(否则可能安装错误)
https://pecl.php.net/package/ssh2

配置php
第一种方式:
vim /etc/php.d/ssh2.ini
输入
extension=ssh2.so

第二种方式:
vim /etc/php.ini
输入
extension=ssh2.so

配置后重启php-fpm

查看是否安装成功
php -i|grep ssh2
<?php

class SFtp
{
    private $_connection;
    private $_sftp;
    private $_error;

    public function __construct($host,$username, $password, $port=22)
    {
        $this->_connection = @ssh2_connect($host, $port);
        if (!$this->_connection) {
            $this->_error = "主机连接失败";
            return false;
        }
        $this->_login($username, $password);
    }

    public function __destruct()
    {
        @ssh2_disconnect($this->_connection);
    }

    /**
     * @description: 登陆sftp
     * @param username string 账号
     * @param password string 密码
     * @return {type} 
     */    
    private function _login($username, $password)
    {
        
        if (! @ssh2_auth_password($this->_connection, $username, $password)) {
            $this->_error = "服务器登录失败";
            return false;
        }
        $this->_sftp = @ssh2_sftp($this->_connection);
        if (!$this->_sftp) {
            $this->_error = "SFTP子系统初始化失败";
            return false;
        }
    }

    /**
     * 上传文件到ftp服务器
     * @param local_file string 本地文件路径
     * @param remote_file string 服务器文件地址 地址要完整 否则无法上传 如/1/123.txt
     * @return {type} 
     */    
    public function upload($local_file, $remote_file)
    {
        $sftp = $this->_sftp;
        $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');
        if (! $stream) {
            $this->_error = "无法打开远程文件";
            return false;
        }

        $data_to_send = @file_get_contents($local_file);
        if ($data_to_send === false) {
            $this->error = "本地文件不存在";
            return false;
        }

        if (@fwrite($stream, $data_to_send) === false) {
            $this->error = "文件上传失败";
            return false;
        }
        @fclose($stream);
    }

    /**
     * 从ftp服务器下载文件到本地
     * @param remote_file string 服务器文件地址 地址要完整 如/1/123.txt
     * @param local_file string 本地文件路径
     * @return {type} 
     */    
    public function download($remote_file,$local_file)
    {
        $sftp = $this->_sftp;
        $data_to_send = @file_get_contents("ssh2.sftp://$sftp$remote_file");
        if ($data_to_send === false) {
            $this->error = "远程文件不存在";
            return false;
        }

        $stream = @fopen($local_file, 'w');
        if (! $stream) {
            $this->_error = "无法打开本地文件";
            return false;
        }

        if (@fwrite($stream, $data_to_send) === false) {
            $this->error = "文件下载失败";
            return false;
        }
        @fclose($stream);
    }

    /**
     * 获取上传错误信息
     */
    public function get_error_msg(){
        return $this->_error;
    }
}
{/if}