php sftp文件上传 文件上传

<?php
/**
 * sftp文件上传
 */

/**
 * Sftp 操作類
 * Class Sftp
 */
class Sftp
{
    
    
    /**
     * 連接對象
     * @var false|resource
     */
    private $connection;
    /**
     * 操作對象
     * @var Sftp
     */
    private $sftp;
    /**
     * ip
     * @var mixed
     */
    private $host;
    /**
     * 端口
     * @var mixed
     */
    private $port;
    /**
     * 賬號
     * @var mixed
     */
    private $username;
    /**
     * 密碼
     * @var mixed
     */
    private $password;

    /**
     * 初始化Sftp連接
     * Sftp constructor.
     * @param $params 鏈接參數
     * @throws Exception
     */
    public function __construct($params)
    {
    
    
        $this->host = $params['host'];
        $this->port = $params['port'];
        $this->username = $params['username'];
        $this->password = $params['password'];
        try{
    
    
            $this->connection = ssh2_connect($this->host, $this->port);
        }catch (Exception $e){
    
    
           
            //Throw an exception Added by Khail
            throw new Exception("{
      
      $this->host} 連接 {
      
      $this->port} 埠失敗");
        }
        if (!$this->connection) {
    
    
            //The connection errors are recorded in the log file Added by Khail
            throw new Exception("{
      
      $this->host} 連接 {
      
      $this->port} 埠失敗");
        }
        $this->login('username');
    }

   
    /**
     * 登錄
     * @param string $login_type 登錄類型
     * @param string $username 用戶名
     * @param string $password 密碼
     * @param string $pub_key 公鑰
     * @param string $pri_key 私鑰
     * @throws Exception]
     */
    public function login($login_type = 'username', $pub_key = null, $pri_key = null)
    {
    
    
        switch ($login_type) {
    
    
            case 'username':    //通過用戶名密碼登錄
                $login_result = ssh2_auth_password($this->connection, $this->username, $this->password);
                break;
            case 'pub_key':     //公鑰私鑰登錄
                $login_result = ssh2_auth_pubkey_file($this->connection, $this->username, $pub_key, $pri_key);
                break;
        }
        if (!$login_result) {
    
    
            //Add authentication failed message to log Added by Khail
            throw new Exception("身份驗證失敗");
        }
        $this->sftp = ssh2_sftp($this->connection);
        if (!$this->sftp) {
    
    
            //'Failed to initialize SFTP' recorded in the log Added by Khail
            throw new Exception("初始化sftp失敗");
        }
        return true;
    }

    /**
     * 上傳文件
     * @param string $local_file 本地文件
     * @param string $remote_file 遠程文件
     * @throws Exception
     */
    public function upload_file($local_file, $remote_file)
    {
    
    
        $sftp = $this->sftp;
        $true = copy($local_file, "ssh2.sftp://$sftp/" . $remote_file);
        return $true;
        // $is_true = ssh2_scp_send($this->connection, $local_file, $remote_file, 0777);
        // return $is_true;
    }

    /**
     * 下載文件
     * @param $local_file 本地文件
     * @param $remote_file 遠程文件
     */
    public function download_file($local_file, $remote_file)
    {
    
    
        $sftp = $this->sftp;
        $resource = "ssh2.sftp://$sftp/" . $remote_file;
        $is_true = copy($resource, $local_file);
        return $is_true;
        // $true = ssh2_scp_recv($this->connection, $remote_file, $local_file);
        // return $true;
    }

    /**
     * 判斷文件夾是否存在
     * @param string $dir 目錄名稱
     * @return bool
     */
    public function dir_exits($dir)
    {
    
    
        return file_exists("ssh2.sftp://$this->sftp" . $dir);
    }


    /**
     * 創建目錄
     * @param string $path 例子  '/home/username/newdir'
     * @param int $auth 默認 0777的權限
     */
    public function mkdir($path, $auth = 0777)  //使用創建目錄循環
    {
    
    
        $end = ssh2_sftp_mkdir($this->sftp, $path, $auth, true);
        if ($end !== true) throw new Exception('文件夾創建失敗');
    }

    /**
     * 目錄重命名
     * @param string $dir 例子:'/home/username/newnamedir'
     * $dir 示例:/var/file/image
     * @return bool
     */
    public function rename($old_dir, $new_dir)
    {
    
    
        $is_true = ssh2_sftp_rename($this->sftp, $old_dir, $new_dir);
        return $is_true;
    }

    /**
     * 刪除文件
     * @param string $dir 例子:'/home/username/dirname/filename'
     * $dir 示例:/var/file/image/404NotFound.png
     * @return bool
     */
    public function del_file($dir)
    {
    
    
        $is_true = ssh2_sftp_unlink($this->sftp, $dir);
        return $is_true;
    }

    /**
     * 獲取文件夾下的文件
     * @param string $remote_file 文件路徑 例:/var/file/image
     * @return array
     */
    public function get_list($remote_file)
    {
    
    
        $sftp = $this->sftp;
        $dir = "ssh2.sftp://$sftp$remote_file";
        $tempArray = array();
        $handle = opendir($dir);
        // 所有的文件列表
        while (false !== ($file = readdir($handle))) {
    
    
            if (substr("$file", 0, 1) != ".") {
    
    
                if (is_dir($file)) {
    
    
//                $tempArray[$file] = $this->scanFilesystem("$dir/$file");
                } else {
    
    
                    $tempArray[] = $file;
                }
            }
        }
        closedir($handle);
        return $tempArray;
    }
}

Guess you like

Origin blog.csdn.net/weixin_31501115/article/details/118760406