国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php中使用sftp教程

2024-05-04 23:33:36
字體:
來源:轉載
供稿:網友

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

  1. <?php  
  2.  
  3.  
  4. /** 
  5. php 中的sftp 使用教程  
  6. Telnet、FTP、SSH、SFTP、SSL  
  7. (一) ftp 協議簡介  
  8.  
  9. FTP(File Transfer Protocol,文件傳輸協議)是互聯網上常用的協議之一,人們用FTP實現互連網上的文件傳輸。 
  10. 如同其他的很多通訊協議,FTP通訊協議也采用客戶機 / 服務器(Client / Server )架構。用戶可以通過各種不同的FTP客戶端程序, 
  11. 借助FTP協議,來連接FTP服務器,以上傳或者下載文件FTP的命令傳輸和數據傳輸是通過不同的端口進行傳輸的 
  12. FTP是TCP/IP的一種具體應用,它工作在OSI模型的第七層,TCP模型的第四層上,即應用層,使用TCP傳輸而不是UDP, 
  13. 這樣FTP客戶在和服 務器建立連接前就要經過一個被廣為熟知的"三次握手"的過程,它帶來的意義在于客戶與服務器之間的連接是可靠的, 
  14. 而且是面向連接,為數據的傳輸提供了可靠 的保證。 
  15.  
  16. (二)ssh協議  
  17.  
  18. ssh 的全稱為 SecureShell ,可以報所有的傳輸數據驚醒加密,這樣'中間人'就不能獲得我們傳輸的數據 
  19. 同事,傳輸的數據是經過壓縮的,可以加快傳輸的速度.ssh有很多功能,可以替代telnet 也可也為ftppop ,提供一個安全的通道  
  20.  
  21. SSH協議框架中最主要的部分是三個協議: 
  22.  
  23. * 傳輸層協議(The Transport Layer Protocol)提供服務器認證,數據機密性,信息完整性 等的支持; 
  24. * 用戶認證協議(The User Authentication Protocol) 則為服務器提供客戶端的身份鑒別; 
  25. * 連接協議(The Connection Protocol) 將加密的信息隧道復用成若干個邏輯通道,提供給更高層的應用協議使用;  
  26. 各種高層應用協議可以相對地獨立于SSH基本體系之外,并依靠這個基本框架,通過連接協議使用SSH的安全機制。 
  27.  
  28. (三)sftp 協議  
  29. 使用SSH協議進行FTP傳輸的協議叫SFTP(安全文件傳輸)Sftp和Ftp都是文件傳輸協議。區別:sftp是ssh內含的協議(ssh是加密的telnet協議), 
  30. 只要sshd服務器啟動了,它就可用,而且sftp安全性較高,它本身不需要ftp服務器啟動。 sftp = ssh + ftp(安全文件傳輸協議)。由于ftp是明文傳輸的, 
  31. 沒有安全性,而sftp基于ssh,傳輸內容是加密過的,較為安全。目前網絡不太安全,以前用telnet的都改用ssh2(SSH1已被破解)。sftp這個工具和ftp用 
  32. 法一樣。但是它的傳輸文件是通過ssl加密了的,即使被截獲了也無法破解。而且sftp相比ftp功能要多一些,多了一些文件屬性的設置 
  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. // ftp 連接  
  52. public function connect(){ 
  53. return $this->conn = ftp_connect($this->config['host'],$this->config['port']));  
  54.  
  55.  
  56. // 傳輸數據 傳輸層協議,獲得數據 true or false  
  57. public function download($remote$local,$mode = 'auto'){ 
  58. return $result = @ftp_get($this->conn, $localpath$remotepath$mode); 
  59.  
  60. // 傳輸數據 傳輸層協議,上傳數據 true or false  
  61. public function upload($remote$local,$mode = 'auto'){ 
  62. return $result = @ftp_put($this->conn, $localpath$remotepath$mode); 
  63.  
  64.  
  65. // 刪除文件  
  66. public function remove($remote){ 
  67. return $result = @ftp_delete($this->conn_id, $file); 
  68.  
  69.  
  70. }  
  71.  
  72.  
  73.  
  74. // 使用  
  75. $config = array
  76. 'hostname' => 'localhost'
  77. 'username' => 'root'
  78. 'password' => 'root'
  79. 'port' => 21 
  80.  
  81. ) ; 
  82.  
  83. $ftp = new Ftp(); 
  84. $ftp->connect($config); 
  85. $ftp->upload('ftp_err.log','ftp_upload.log'); 
  86. $ftp->download('ftp_upload.log','ftp_download.log'); 
  87.  
  88.  
  89.  
  90. /*根據上面的三個協議寫出基于ssh 的ftp 類 
  91. 我們知道進行身份認證的方式有兩種:公鑰;密碼 ; 
  92. (1) 使用密碼登陸 
  93. (2) 免密碼登陸也就是使用公鑰登陸  
  94.  
  95. */ 
  96.  
  97. class sftp{ 
  98.  
  99.  
  100. // 初始配置為NULL 
  101. private $config =NULL ; 
  102. // 連接為NULL  
  103. private $conn = NULL; 
  104.  
  105.  
  106. // 是否使用秘鑰登陸  
  107. private $use_pubkey_file= false; 
  108.  
  109. // 初始化 
  110. public function init($config){ 
  111. $this->config = $config ;  
  112.  
  113.  
  114. // 連接ssh ,連接有兩種方式(1) 使用密碼 
  115. // (2) 使用秘鑰  
  116. public function connect(){ 
  117.  
  118. $methods['hostkey'] = $use_pubkey_file ? 'ssh-rsa' : [] ;  
  119. $con = ssh2_connect($this->config['host'], $this->config['port'], $methods); 
  120. //(1) 使用秘鑰的時候  
  121. if($use_pubkey_file){ 
  122. // 用戶認證協議 
  123. $rc = ssh2_auth_pubkey_file( 
  124. $conn
  125. $this->config['user'], 
  126. $this->config['pubkey_file'], 
  127. $this->config['privkey_file'], 
  128. $this->config['passphrase'])  
  129. ); 
  130. //(2) 使用登陸用戶名字和登陸密碼 
  131. }else
  132. $rc = ssh2_auth_password( $conn$this->conf_['user'],$this->conf_['passwd']); 
  133.  
  134.  
  135. return $rc ;  
  136.  
  137.  
  138. // 傳輸數據 傳輸層協議,獲得數據 
  139. public function download($remote$local){ 
  140.  
  141. return ssh2_scp_recv($this->conn_, $remote$local); 
  142.  
  143. //傳輸數據 傳輸層協議,寫入ftp服務器數據 
  144. public function upload($remote$local,$file_mode=0664){ 
  145. return ssh2_scp_send($this->conn_, $local$remote$file_mode); 
  146.  
  147.  
  148. // 刪除文件  
  149. public function remove($remote){ 
  150. $sftp = ssh2_sftp($this->conn_); 
  151. $rc = false; 
  152.  
  153. if (is_dir("ssh2.sftp://{$sftp}/{$remote}")) { 
  154. $rc = false ; 
  155.  
  156. // ssh 刪除文件夾 
  157. $rc = ssh2_sftp_rmdir($sftp$remote); 
  158. else { 
  159. // 刪除文件 
  160. $rc = ssh2_sftp_unlink($sftp$remote); 
  161. return $rc
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169. $config = [ 
  170. "host" => "192.168.1.1 "// ftp地址 
  171. "user" => "***",  
  172. "port" => "22"
  173. "pubkey_path" => "/root/.ssh/id_rsa.pub"// 公鑰的存儲地址 
  174. "privkey_path" => "/root/.ssh/id_rsa"// 私鑰的存儲地址 
  175. ]; 
  176.  
  177. $handle = new SftpAccess(); 
  178. $handle->init($config); 
  179. $rc = $handle->connect(); 
  180. $handle->getData(remote, $local); 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 神木县| 新邵县| 佛教| 托克托县| 芜湖县| 乡宁县| 乳山市| 天门市| 金堂县| 同德县| 宜兰市| 临潭县| 乡宁县| 长丰县| 庄浪县| 兴和县| 柏乡县| 垫江县| 胶南市| 恩平市| 孝感市| 开平市| 上饶县| 长顺县| 德令哈市| 瑞安市| 长乐市| 洞头县| 嵊泗县| 常州市| 鄂托克旗| 石门县| 大连市| 定兴县| 宜兰市| 交口县| 沛县| 玉门市| 扶绥县| 乐昌市| 哈巴河县|