经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
php连接sftp的作用以及实例代码
来源:jb51  时间:2019/9/24 8:32:53  对本文有异议

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功能要多一些,多了一些文件属性的设置

  1. // 注意这里只是为了介绍ftp ,并没有做验证 ;
  2.  
  3. class ftp{
  4.  
  5.  
  6. // 初始配置为NULL
  7.  
  8. private $config =NULL ;
  9.  
  10. // 连接为NULL
  11.  
  12. private $conn = NULL;
  13.  
  14.  
  15. public function init($config){
  16.  
  17. $this->config = $config;
  18.  
  19. }
  20.  
  21.  
  22. // ftp 连接
  23.  
  24. public function connect(){
  25.  
  26. return $this->conn = ftp_connect($this->config['host'],$this->config['port']));
  27.  
  28. }
  29.  
  30.  
  31.  
  32. // 传输数据 传输层协议,获得数据 true or false
  33.  
  34. public function download($remote, $local,$mode = 'auto'){
  35.  
  36. return $result = @ftp_get($this->conn, $localpath, $remotepath, $mode);
  37.  
  38. }
  39.  
  40.  
  41. // 传输数据 传输层协议,上传数据 true or false
  42.  
  43. public function upload($remote, $local,$mode = 'auto'){
  44.  
  45. return $result = @ftp_put($this->conn, $localpath, $remotepath, $mode);
  46.  
  47. }
  48.  
  49.  
  50.  
  51. // 删除文件
  52.  
  53. public function remove($remote){
  54.  
  55. return $result = @ftp_delete($this->conn_id, $file);
  56.  
  57. }
  58.  
  59.  
  60.  
  61. }
  62.  
  63.  
  64.  
  65.  
  66. // 使用
  67.  
  68. $config = array(
  69.  
  70. 'hostname' => 'localhost',
  71.  
  72. 'username' => 'root',
  73.  
  74. 'password' => 'root',
  75.  
  76. 'port' => 21
  77.  
  78.  
  79. ) ;
  80.  
  81.  
  82. $ftp = new Ftp();
  83.  
  84. $ftp->connect($config);
  85.  
  86. $ftp->upload('ftp_err.log','ftp_upload.log');
  87.  
  88. $ftp->download('ftp_upload.log','ftp_download.log');
  89.  
  90.  
  91.  
  92.  
  93. /*根据上面的三个协议写出基于ssh 的ftp 类
  94.  
  95. 我们知道进行身份认证的方式有两种:公钥;密码 ;
  96.  
  97. (1) 使用密码登陆
  98.  
  99. (2) 免密码登陆也就是使用公钥登陆
  100.  
  101.  
  102. */
  103.  
  104.  
  105. class sftp{
  106.  
  107.  
  108.  
  109. // 初始配置为NULL
  110.  
  111. private $config =NULL ;
  112.  
  113. // 连接为NULL
  114.  
  115. private $conn = NULL;
  116.  
  117.  
  118.  
  119. // 是否使用秘钥登陆
  120.  
  121. private $use_pubkey_file= false;
  122.  
  123.  
  124. // 初始化
  125.  
  126. public function init($config){
  127.  
  128. $this->config = $config ;
  129.  
  130. }
  131.  
  132.  
  133.  
  134. // 连接ssh ,连接有两种方式(1) 使用密码
  135.  
  136. // (2) 使用秘钥
  137.  
  138. public function connect(){
  139.  
  140.  
  141. $methods['hostkey'] = $use_pubkey_file ? 'ssh-rsa' : [] ;
  142.  
  143. $con = ssh2_connect($this->config['host'], $this->config['port'], $methods);
  144.  
  145. //(1) 使用秘钥的时候
  146.  
  147. if($use_pubkey_file){
  148.  
  149. // 用户认证协议
  150.  
  151. $rc = ssh2_auth_pubkey_file(
  152.  
  153. $conn,
  154.  
  155. $this->config['user'],
  156.  
  157. $this->config['pubkey_file'],
  158.  
  159. $this->config['privkey_file'],
  160.  
  161. $this->config['passphrase'])
  162.  
  163. );
  164.  
  165. //(2) 使用登陆用户名字和登陆密码
  166.  
  167. }else{
  168.  
  169. $rc = ssh2_auth_password( $conn, $this->conf_['user'],$this->conf_['passwd']);
  170.  
  171.  
  172. }
  173.  
  174.  
  175. return $rc ;
  176.  
  177. }
  178.  
  179.  
  180.  
  181. // 传输数据 传输层协议,获得数据
  182.  
  183. public function download($remote, $local){
  184.  
  185.  
  186. return ssh2_scp_recv($this->conn_, $remote, $local);
  187.  
  188. }
  189.  
  190.  
  191. //传输数据 传输层协议,写入ftp服务器数据
  192.  
  193. public function upload($remote, $local,$file_mode=0664){
  194.  
  195. return ssh2_scp_send($this->conn_, $local, $remote, $file_mode);
  196.  
  197.  
  198. }
  199.  
  200.  
  201. // 删除文件
  202.  
  203. public function remove($remote){
  204.  
  205. $sftp = ssh2_sftp($this->conn_);
  206.  
  207. $rc = false;
  208.  
  209.  
  210. if (is_dir("ssh2.sftp://{$sftp}/{$remote}")) {
  211.  
  212. $rc = false ;
  213.  
  214.  
  215. // ssh 删除文件夹
  216.  
  217. $rc = ssh2_sftp_rmdir($sftp, $remote);
  218.  
  219. } else {
  220.  
  221. // 删除文件
  222.  
  223. $rc = ssh2_sftp_unlink($sftp, $remote);
  224.  
  225. }
  226.  
  227. return $rc;
  228.  
  229.  
  230. }
  231.  
  232.  
  233.  
  234.  
  235.  
  236. }
  237.  
  238.  
  239.  
  240. $config = [
  241.  
  242. "host" => "192.168.1.1 ", // ftp地址
  243.  
  244. "user" => "***",
  245.  
  246. "port" => "22",
  247.  
  248. "pubkey_path" => "/root/.ssh/id_rsa.pub", // 公钥的存储地址
  249.  
  250. "privkey_path" => "/root/.ssh/id_rsa", // 私钥的存储地址
  251.  
  252. ];
  253.  
  254.  
  255. $handle = new SftpAccess();
  256.  
  257. $handle->init($config);
  258.  
  259. $rc = $handle->connect();
  260.  
  261. $handle->getData(remote, $local);

以上就是本次介绍的全部知识点内容,感谢大家的学习和对w3xue的支持。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号