php中使用sftp教程

字號(hào):


    這篇文章主要介紹了php中使用sftp教程,本文講解了ftp 協(xié)議簡(jiǎn)介、ssh協(xié)議、sftp 協(xié)議等知識(shí),并給出了FTP和SFTP操作類(lèi)實(shí)現(xiàn)代碼,需要的朋友可以參考下
    <?php
    /**
    php 中的sftp 使用教程
    Telnet、FTP、SSH、SFTP、SSL
    (一) ftp 協(xié)議簡(jiǎn)介
    FTP(File Transfer Protocol,文件傳輸協(xié)議)是互聯(lián)網(wǎng)上常用的協(xié)議之一,人們用FTP實(shí)現(xiàn)互連網(wǎng)上的文件傳輸。
    如同其他的很多通訊協(xié)議,F(xiàn)TP通訊協(xié)議也采用客戶機(jī) / 服務(wù)器(Client / Server )架構(gòu)。用戶可以通過(guò)各種不同的FTP客戶端程序,
    借助FTP協(xié)議,來(lái)連接FTP服務(wù)器,以上傳或者下載文件FTP的命令傳輸和數(shù)據(jù)傳輸是通過(guò)不同的端口進(jìn)行傳輸?shù)?BR>    FTP是TCP/IP的一種具體應(yīng)用,它工作在OSI模型的第七層,TCP模型的第四層上,即應(yīng)用層,使用TCP傳輸而不是UDP,
    這樣FTP客戶在和服 務(wù)器建立連接前就要經(jīng)過(guò)一個(gè)被廣為熟知的"三次握手"的過(guò)程,它帶來(lái)的意義在于客戶與服務(wù)器之間的連接是可靠的,
    而且是面向連接,為數(shù)據(jù)的傳輸提供了可靠 的保證。
    (二)ssh協(xié)議
    ssh 的全稱為 SecureShell ,可以報(bào)所有的傳輸數(shù)據(jù)驚醒加密,這樣'中間人'就不能獲得我們傳輸?shù)臄?shù)據(jù)
    同事,傳輸?shù)臄?shù)據(jù)是經(jīng)過(guò)壓縮的,可以加快傳輸?shù)乃俣?ssh有很多功能,可以替代telnet 也可也為ftppop ,提供一個(gè)安全的通道
    SSH協(xié)議框架中最主要的部分是三個(gè)協(xié)議:
    * 傳輸層協(xié)議(The Transport Layer Protocol)提供服務(wù)器認(rèn)證,數(shù)據(jù)機(jī)密性,信息完整性 等的支持;
    * 用戶認(rèn)證協(xié)議(The User Authentication Protocol) 則為服務(wù)器提供客戶端的身份鑒別;
    * 連接協(xié)議(The Connection Protocol) 將加密的信息隧道復(fù)用成若干個(gè)邏輯通道,提供給更高層的應(yīng)用協(xié)議使用;
    各種高層應(yīng)用協(xié)議可以相對(duì)地獨(dú)立于SSH基本體系之外,并依靠這個(gè)基本框架,通過(guò)連接協(xié)議使用SSH的安全機(jī)制。
    (三)sftp 協(xié)議
    使用SSH協(xié)議進(jìn)行FTP傳輸?shù)膮f(xié)議叫SFTP(安全文件傳輸)Sftp和Ftp都是文件傳輸協(xié)議。區(qū)別:sftp是ssh內(nèi)含的協(xié)議(ssh是加密的telnet協(xié)議),
    只要sshd服務(wù)器啟動(dòng)了,它就可用,而且sftp安全性較高,它本身不需要ftp服務(wù)器啟動(dòng)。 sftp = ssh + ftp(安全文件傳輸協(xié)議)。由于ftp是明文傳輸?shù)模?BR>    沒(méi)有安全性,而sftp基于ssh,傳輸內(nèi)容是加密過(guò)的,較為安全。目前網(wǎng)絡(luò)不太安全,以前用telnet的都改用ssh2(SSH1已被破解)。sftp這個(gè)工具和ftp用
    法一樣。但是它的傳輸文件是通過(guò)ssl加密了的,即使被截獲了也無(wú)法破解。而且sftp相比f(wàn)tp功能要多一些,多了一些文件屬性的設(shè)置
    */
    // 注意這里只是為了介紹ftp ,并沒(méi)有做驗(yàn)證 ;
    class ftp{
    // 初始配置為NULL
    private $config =NULL ;
    // 連接為NULL
    private $conn = NULL;
    public function init($config){
    $this->config = $config;
    }
    // ftp 連接
    public function connect(){
    return $this->conn = ftp_connect($this->config['host'],$this->config['port']));
    }
    // 傳輸數(shù)據(jù) 傳輸層協(xié)議,獲得數(shù)據(jù) true or false
    public function download($remote, $local,$mode = 'auto'){
    return $result = @ftp_get($this->conn, $localpath, $remotepath, $mode);
    }
    // 傳輸數(shù)據(jù) 傳輸層協(xié)議,上傳數(shù)據(jù) 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');
    /*根據(jù)上面的三個(gè)協(xié)議寫(xiě)出基于ssh 的ftp 類(lèi)
    我們知道進(jìn)行身份認(rèn)證的方式有兩種:公鑰;密碼 ;
    (1) 使用密碼登陸
    (2) 免密碼登陸也就是使用公鑰登陸
    */
    class sftp{
    // 初始配置為NULL
    private $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) 使用秘鑰的時(shí)候
    if($use_pubkey_file){
    // 用戶認(rèn)證協(xié)議
    $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 ;
    }
    // 傳輸數(shù)據(jù) 傳輸層協(xié)議,獲得數(shù)據(jù)
    public function download($remote, $local){
    return ssh2_scp_recv($this->conn_, $remote, $local);
    }
    //傳輸數(shù)據(jù) 傳輸層協(xié)議,寫(xiě)入ftp服務(wù)器數(shù)據(jù)
    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", // 公鑰的存儲(chǔ)地址
    "privkey_path" => "/root/.ssh/id_rsa", // 私鑰的存儲(chǔ)地址
    ];
    $handle = new SftpAccess();
    $handle->init($config);
    $rc = $handle->connect();
    $handle->getData(remote, $local);