php 中的sftp 使用教程_PHP教程

來源:互聯網
上載者:User

php 中的sftp 使用教程


config = $config; }// ftp 串連 public function connect(){return  $this->conn = ftp_connect($this->config['host'],$this->config['port'])); }// 傳輸資料  傳輸層協議,獲得資料 true or false    public function download($remote, $local,$mode = 'auto'){   return $result = @ftp_get($this->conn, $localpath, $remotepath, $mode);   }      // 傳輸資料  傳輸層協議,上傳資料 true or false    public function upload($remote, $local,$mode = 'auto'){   return $result = @ftp_put($this->conn, $localpath, $remotepath, $mode);   }          // 刪除檔案  public function remove($remote){  return  $result = @ftp_delete($this->conn_id, $file);}   }// 使用 $config = array('hostname' => 'localhost',            'username' => 'root',            'password' => 'root',            'port' => 21) ; $ftp = new Ftp();$ftp->connect($config);$ftp->upload('ftp_err.log','ftp_upload.log');$ftp->download('ftp_upload.log','ftp_download.log');/*根據上面的三個協議寫出基於ssh 的ftp 類我們知道進行身份認證的方式有兩種:公開金鑰;密碼 ;(1) 使用密碼登陸(2) 免密碼登陸也就是使用公開金鑰登陸 */class sftp{// 初始配置為NULLprivate  $config =NULL ;// 串連為NULL private  $conn = NULL;// 是否使用秘鑰登陸  private $use_pubkey_file= false;// 初始化public function init($config){$this->config = $config ; }// 串連ssh ,串連有兩種方式(1) 使用密碼// (2)  使用秘鑰 public function connect(){$methods['hostkey'] =  $use_pubkey_file ?  'ssh-rsa'  : [] ; $con = ssh2_connect($this->config['host'], $this->config['port'], $methods);//(1) 使用秘鑰的時候 if($use_pubkey_file){//  使用者認證協議 $rc = ssh2_auth_pubkey_file($conn,$this->config['user'],$this->config['pubkey_file'],$this->config['privkey_file'],$this->config['passphrase']) );//(2) 使用登陸使用者名稱字和登陸密碼}else{$rc = ssh2_auth_password( $conn, $this->conf_['user'],$this->conf_['passwd']);            }return $rc ;  }// 傳輸資料  傳輸層協議,獲得資料   public function download($remote, $local){       return ssh2_scp_recv($this->conn_, $remote, $local);   }     //傳輸資料  傳輸層協議,寫入ftp伺服器資料  public function upload($remote, $local,$file_mode=0664){   return ssh2_scp_send($this->conn_, $local, $remote, $file_mode);      }    // 刪除檔案      public function remove($remote){$sftp = ssh2_sftp($this->conn_);$rc   = false;        if (is_dir(ssh2.sftp://{$sftp}/{$remote})) {$rc = false ;// ssh 刪除檔案夾            $rc = ssh2_sftp_rmdir($sftp, $remote);} else {    // 刪除檔案$rc = ssh2_sftp_unlink($sftp, $remote);}return $rc;}     }$config  = [    host          => 192.168.1.1 ,      //  ftp地址    user          => ***,     port          => 22,    pubkey_path  => /root/.ssh/id_rsa.pub,   // 公開金鑰的儲存地址    privkey_path => /root/.ssh/id_rsa,          // 私密金鑰的儲存地址];$handle = new SftpAccess();$handle->init($config);$rc = $handle->connect();$handle->getData(remote, $local);/* 

http://www.bkjia.com/PHPjc/962645.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/962645.htmlTechArticlephp 中的sftp 使用教程 config = $config; }// ftp 串連 public function connect(){return $this-conn = ftp_connect($this-config[host],$this-config[port])); }// 傳輸資料 傳輸...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.