zoukankan      html  css  js  c++  java
  • THINKPHP 3.2 PHP SFTP上传下载 代码实现方法

     一、SFTP介绍:使用SSH协议进行FTP传输的协议叫SFTP(安全文件传输)Sftp和Ftp都是文件传输协议。区别:sftp是ssh内含的协议(ssh是加密的telnet协议),
     只要sshd服务器启动了,它就可用,而且sftp安全性较高,它本身不需要ftp服务器启动。 sftp = ssh + ftp(安全文件传输协议)。由于ftp是明文传输的,
     没有安全性,而sftp基于ssh,传输内容是加密过的,较为安全。目前网络不太安全,以前用telnet的都改用ssh2(SSH1已被破解)。sftp这个工具和ftp用
     法一样。但是它的传输文件是通过ssl加密了的,即使被截获了也无法破解。而且sftp相比ftp功能要多一些,多了一些文件属性的设置。
     
      二、SSH2扩展配置
       1.   下载地址: http://windows.php.net/downloads/pecl/releases/ssh2/0.12/
          根据自己的php版本选择 扩展包,这里我使用的是php5.3,所以我下载的是 php_ssh2-0.12-5.3-ts-vc9-x86.zip
    2. 解压完后,会有三个文件,libssh2.dll、php_ssh.dll、php_ssh2.pdb。
     
    3. 将 php_ssh.dll、php_ssh2.pdb 放到你的 php 扩展目录下 php/ext/ 下。
     
    4. 将libssh2.dll 复制到 c:/windows/system32 和 c:/windows/syswow64 各一份
     
    5.在 php.ini中加入 extension=php_ssh2.dll

    6.重启Apache, 打印phpinfo(); 会出现 SSH2 扩展,表示安装成功

    三、SFTP 代码DEMO

       

     调用代码
     $config = array(
                'host'     =>'211.*.*.*', //服务器
                'port'     => '23', //端口
                'username' =>'test', //用户名
                'password' =>'*****', //密码
            );
            $ftp = new Sftp($config);
            $localpath="E:/www/new_20170724.csv";
            $serverpath='/new_20170724.csv';
            $st = $ftp->upftp($localpath,$serverpath);     //上传指定文件
            if($st == true){
                echo "success";
                
            }else{
                echo "fail";
            }

    SFTP 封装类

     1 <?php
     2 /**
     3  * SFtp上传下载文件
     4  *
     5  */
     6 namespace CommonORGUtil;
     7 
     8 class Sftp
     9 {
    10     
    11     // 初始配置为NULL
    12     private $config = NULL;
    13     // 连接为NULL
    14     private $conn = NULL;
    15     // 初始化
    16     public function __construct($config)
    17     {
    18         $this->config = $config;
    19         $this->connect();
    20     }
    21     
    22 
    23     public function connect()
    24     {
    25         
    26         $this->conn = ssh2_connect($this->config['host'], $this->config['port']);
    27         if( ssh2_auth_password($this->conn, $this->config['username'], $this->config['password']))
    28         {
    29            
    30         }else{ 
    31             echo "无法在服务器进行身份验证";
    32         }
    33     
    34     }
    35     
    36     // 传输数据 传输层协议,获得数据
    37     public function downftp($remote, $local)
    38     {   
    39         $ressftp = ssh2_sftp($this->conn);
    40         return copy("ssh2.sftp://{$ressftp}".$remote, $local);
    41     }
    42     
    43     // 传输数据 传输层协议,写入ftp服务器数据
    44     public function upftp( $local,$remote, $file_mode = 0777)
    45     {   
    46         $ressftp = ssh2_sftp($this->conn);
    47         return  copy($local,"ssh2.sftp://{$ressftp}".$remote);  
    48         
    49     }
    50   
    51 }
     
  • 相关阅读:
    jmeter(十八)属性和变量
    jmeter(十七)逻辑控制器
    jmeter(九)分布式测试
    jmeter(八)HTTP属性管理器HTTP Cookie Manager、HTTP Request Defaults
    jmeter(七)函数
    jmeter(六)关联
    jmeter(五)集合点
    jmeter(四)检查点
    jmeter(三)参数传递
    jmeter(二)元件的作用域与执行顺序
  • 原文地址:https://www.cnblogs.com/kingchou/p/7238276.html
Copyright © 2011-2022 走看看