日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

php中使用sftp教程_PHP教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:php計算一個文件大小的方法
這篇文章主要介紹了php計算一個文件大小的方法,涉及php操作文件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下 本文實例講述了php計算一個文件大小的方法。分享給大家供大家參考。具體如下: ? 希望本文所述對大家的php程序設(shè)計有所幫助。

這篇文章主要介紹了php中使用sftp教程,本文講解了ftp 協(xié)議簡介、ssh協(xié)議、sftp 協(xié)議等知識,并給出了FTP和SFTP操作類實現(xiàn)代碼,需要的朋友可以參考下

  1. <?php  
  2.   
  3.   
  4. /** 
  5. php 中的sftp 使用教程  
  6. Telnet、FTP、SSH、SFTP、SSL  
  7. (一) ftp 協(xié)議簡介  
  8.   
  9.     FTP(File Transfer Protocol,文件傳輸協(xié)議)是互聯(lián)網(wǎng)上常用的協(xié)議之一,人們用FTP實現(xiàn)互連網(wǎng)上的文件傳輸。 
  10. 如同其他的很多通訊協(xié)議,F(xiàn)TP通訊協(xié)議也采用客戶機 / 服務(wù)器(Client / Server )架構(gòu)。用戶可以通過各種不同的FTP客戶端程序, 
  11. 借助FTP協(xié)議,來連接FTP服務(wù)器,以上傳或者下載文件FTP的命令傳輸和數(shù)據(jù)傳輸是通過不同的端口進行傳輸?shù)?/span> 
  12. FTP是TCP/IP的一種具體應(yīng)用,它工作在OSI模型的第七層,TCP模型的第四層上,即應(yīng)用層,使用TCP傳輸而不是UDP, 
  13. 這樣FTP客戶在和服 務(wù)器建立連接前就要經(jīng)過一個被廣為熟知的"三次握手"的過程,它帶來的意義在于客戶與服務(wù)器之間的連接是可靠的, 
  14. 而且是面向連接,為數(shù)據(jù)的傳輸提供了可靠 的保證。 
  15.   
  16. (二)ssh協(xié)議  
  17.   
  18.     ssh 的全稱為 SecureShell  ,可以報所有的傳輸數(shù)據(jù)驚醒加密,這樣'中間人'就不能獲得我們傳輸?shù)臄?shù)據(jù) 
  19. 同事,傳輸?shù)臄?shù)據(jù)是經(jīng)過壓縮的,可以加快傳輸?shù)乃俣?ssh有很多功能,可以替代telnet 也可也為ftppop ,提供一個安全的通道  
  20.   
  21.   SSH協(xié)議框架中最主要的部分是三個協(xié)議: 
  22.    
  23. * 傳輸層協(xié)議(The Transport Layer Protocol)提供服務(wù)器認(rèn)證,數(shù)據(jù)機密性,信息完整性 等的支持; 
  24. * 用戶認(rèn)證協(xié)議(The User Authentication Protocol) 則為服務(wù)器提供客戶端的身份鑒別; 
  25. * 連接協(xié)議(The Connection Protocol) 將加密的信息隧道復(fù)用成若干個邏輯通道,提供給更高層的應(yīng)用協(xié)議使用;  
  26.  各種高層應(yīng)用協(xié)議可以相對地獨立于SSH基本體系之外,并依靠這個基本框架,通過連接協(xié)議使用SSH的安全機制。 
  27.    
  28.  (三)sftp 協(xié)議  
  29.   使用SSH協(xié)議進行FTP傳輸?shù)膮f(xié)議叫SFTP(安全文件傳輸)Sftp和Ftp都是文件傳輸協(xié)議。區(qū)別:sftp是ssh內(nèi)含的協(xié)議(ssh是加密的telnet協(xié)議), 
  30.     只要sshd服務(wù)器啟動了,它就可用,而且sftp安全性較高,它本身不需要ftp服務(wù)器啟動。 sftp = ssh + ftp(安全文件傳輸協(xié)議)。由于ftp是明文傳輸?shù)模?/span> 
  31.     沒有安全性,而sftp基于ssh,傳輸內(nèi)容是加密過的,較為安全。目前網(wǎng)絡(luò)不太安全,以前用telnet的都改用ssh2(SSH1已被破解)。sftp這個工具和ftp用 
  32.     法一樣。但是它的傳輸文件是通過ssl加密了的,即使被截獲了也無法破解。而且sftp相比ftp功能要多一些,多了一些文件屬性的設(shè)置 
  33.   
  34.       
  35.     */ 
  36.       
  37.   
  38.   
  39.           
  40. // 注意這里只是為了介紹ftp ,并沒有做驗證 ;       
  41. class ftp{ 
  42.       
  43.     // 初始配置為NULL 
  44.     private $config =NULL ; 
  45.     // 連接為NULL  
  46.     private $conn = NULL; 
  47.       
  48.     public function init($config){ 
  49.      $this->config = $config;     
  50.     } 
  51.       
  52.     // ftp 連接  
  53.     public function connect(){ 
  54.         return $this->conn = ftp_connect($this->config['host'],$this->config['port']));  
  55.     } 
  56.       
  57.       
  58.     // 傳輸數(shù)據(jù) 傳輸層協(xié)議,獲得數(shù)據(jù) true or false  
  59.   public function download($remote$local,$mode = 'auto'){ 
  60.       return $result = @ftp_get($this->conn, $localpath$remotepath$mode); 
  61.   } 
  62.     
  63.   // 傳輸數(shù)據(jù) 傳輸層協(xié)議,上傳數(shù)據(jù) true or false  
  64.   public function upload($remote$local,$mode = 'auto'){ 
  65.       return $result = @ftp_put($this->conn, $localpath$remotepath$mode); 
  66.   } 
  67.     
  68.     
  69.      // 刪除文件  
  70.     public function remove($remote){ 
  71.      return $result = @ftp_delete($this->conn_id, $file); 
  72.     } 
  73.     
  74.       
  75. }        
  76.   
  77.   
  78.   
  79. // 使用  
  80. $config = array
  81.             'hostname' => 'localhost'
  82.       'username' => 'root'
  83.       'password' => 'root'
  84.       'port' => 21 
  85.   
  86. ) ; 
  87.    
  88. $ftp = new Ftp(); 
  89. $ftp->connect($config); 
  90. $ftp->upload('ftp_err.log','ftp_upload.log'); 
  91. $ftp->download('ftp_upload.log','ftp_download.log'); 
  92.   
  93.   
  94.   
  95. /*根據(jù)上面的三個協(xié)議寫出基于ssh 的ftp 類 
  96. 我們知道進行身份認(rèn)證的方式有兩種:公鑰;密碼 ; 
  97. (1) 使用密碼登陸 
  98. (2) 免密碼登陸也就是使用公鑰登陸  
  99.   
  100. */ 
  101.   
  102. class sftp{ 
  103.       
  104.       
  105.     // 初始配置為NULL 
  106.     private $config =NULL ; 
  107.     // 連接為NULL  
  108.     private $conn = NULL; 
  109.   
  110.       
  111.     // 是否使用秘鑰登陸  
  112.      private $use_pubkey_file= false; 
  113.       
  114.     // 初始化 
  115.     public function init($config){ 
  116.         $this->config = $config ;  
  117.     } 
  118.       
  119.       
  120.     // 連接ssh ,連接有兩種方式(1) 使用密碼 
  121.     // (2) 使用秘鑰  
  122.     public function connect(){ 
  123.           
  124.         $methods['hostkey'] = $use_pubkey_file ? 'ssh-rsa' : [] ;  
  125.         $con = ssh2_connect($this->config['host'], $this->config['port'], $methods); 
  126.         //(1) 使用秘鑰的時候  
  127.         if($use_pubkey_file){ 
  128.         // 用戶認(rèn)證協(xié)議 
  129.              $rc = ssh2_auth_pubkey_file( 
  130.                 $conn
  131.                 $this->config['user'], 
  132.                 $this->config['pubkey_file'], 
  133.                 $this->config['privkey_file'], 
  134.                 $this->config['passphrase'])  
  135.             ); 
  136.         //(2) 使用登陸用戶名字和登陸密碼 
  137.         }else
  138.             $rc = ssh2_auth_password( $conn$this->conf_['user'],$this->conf_['passwd']); 
  139.         
  140.         } 
  141.           
  142.         return $rc ;  
  143.     } 
  144.       
  145.       
  146.     // 傳輸數(shù)據(jù) 傳輸層協(xié)議,獲得數(shù)據(jù) 
  147.       public function download($remote$local){ 
  148.             
  149.           return ssh2_scp_recv($this->conn_, $remote$local); 
  150.       } 
  151.         
  152.      //傳輸數(shù)據(jù) 傳輸層協(xié)議,寫入ftp服務(wù)器數(shù)據(jù) 
  153.      public function upload($remote$local,$file_mode=0664){ 
  154.           return ssh2_scp_send($this->conn_, $local$remote$file_mode); 
  155.             
  156.      } 
  157.        
  158.      // 刪除文件  
  159.       public function remove($remote){ 
  160.             $sftp = ssh2_sftp($this->conn_); 
  161.             $rc  = false; 
  162.   
  163.     if (is_dir("ssh2.sftp://{$sftp}/{$remote}")) { 
  164.             $rc = false ; 
  165.               
  166.             // ssh 刪除文件夾 
  167.       $rc = ssh2_sftp_rmdir($sftp$remote); 
  168.             } else { 
  169.           // 刪除文件 
  170.                 $rc = ssh2_sftp_unlink($sftp$remote); 
  171.             } 
  172.             return $rc
  173.               
  174.         } 
  175.            
  176.    
  177.    
  178.       
  179.   
  180.   
  181. $config = [ 
  182.   "host"     => "192.168.1.1 ",   // ftp地址 
  183.   "user"     => "***",  
  184.   "port"     => "22"
  185.   "pubkey_path" => "/root/.ssh/id_rsa.pub",  // 公鑰的存儲地址 
  186.   "privkey_path" => "/root/.ssh/id_rsa",     // 私鑰的存儲地址 
  187. ]; 
  188.   
  189. $handle = new SftpAccess(); 
  190. $handle->init($config); 
  191. $rc = $handle->connect(); 
  192. $handle->getData(remote, $local); 
  193.           
  194.   

分享:smarty模板引擎之內(nèi)建函數(shù)用法
這篇文章主要介紹了smarty模板引擎之內(nèi)建函數(shù)用法,實例分析了smarty中foreach函數(shù)、if...else...、if...elseif...elseif...else...等內(nèi)建函數(shù)的使用方法,具有一定參考借鑒價值,需要的朋友可以參考下 本文實例講述了smarty內(nèi)建函數(shù)的使用方法。分享給大家供大家參考。具

來源:模板無憂//所屬分類:PHP教程/更新時間:2015-04-01
相關(guān)PHP教程