Home > Backend Development > PHP Tutorial > PHP SFTP implements upload and download function code strength sharing

PHP SFTP implements upload and download function code strength sharing

巴扎黑
Release: 2023-03-15 20:34:01
Original
1627 people have browsed it

This article mainly introduces the upload and download functions of PHP SFTP in detail, which has certain reference value. Interested friends can refer to it

1. Introduction to SFTP:

The protocol that uses the SSH protocol for FTP transmission is called SFTP (Secure File Transfer). Sftp and Ftp are both file transfer protocols. Difference: sftp is a protocol included in ssh (ssh is an encrypted telnet protocol). As long as the sshd server is started, it is available. Moreover, sftp is more secure and does not require the ftp server to be started. sftp = ssh + ftp (Secure File Transfer Protocol). Since ftp transmits plain text, there is no security, while sftp is based on ssh, and the transmission content is encrypted, which is more secure. The current network is not very secure. Those who used telnet in the past have switched to ssh2 (SSH1 has been cracked).

The sftp tool is used the same as ftp. However, its transmitted files are encrypted through SSL and cannot be cracked even if they are intercepted. Moreover, sftp has more functions than ftp, including some additional file attribute settings.

2. SSH2 extension configuration

1. Download address: http://windows.php.net/downloads/pecl/releases/ ssh2/0.12/

Choose the expansion package according to your php version. Here I am using php5.3, so I downloaded php_ssh2-0.12-5.3-ts-vc9-x86.zip ( Download link)

#2. After decompression, there will be three files, libssh2.dll, php_ssh.dll, php_ssh2.pdb.

3. Place php_ssh.dll and php_ssh2.pdb under your php extension directory php/ext/.

4. Copy libssh2.dll to c:/windows/system32 and c:/windows/syswow64

5. Add it to php.ini extension=php_ssh2.dll

6. Restart Apache and print phpinfo(); The SSH2 extension will appear, indicating that the installation is successful

3. SFTP code DEMO

Calling code


 $config = array(
  'host' =>'211.*.*.*', //服务器
  'port' => '23', //端口
  'username' =>'test', //用户名
  'password' =>'*****', //密码
 );
 $ftp = new Sftp($config);
 $localpath="E:/www/new_20170724.csv";
 $serverpath='/new_20170724.csv';
 $st = $ftp->upftp($localpath,$serverpath); //上传指定文件
 if($st == true){
  echo "success";
  
 }else{
  echo "fail";
 }
Copy after login

SFTP encapsulation class


<?php
/**
 * SFtp上传下载文件
 *
 */
namespace Common\ORG\Util;

class Sftp
{
 
 // 初始配置为NULL
 private $config = NULL;
 // 连接为NULL
 private $conn = NULL;
 // 初始化
 public function __construct($config)
 {
 $this->config = $config;
 $this->connect();
 }
 

 public function connect()
 {
 
 $this->conn = ssh2_connect($this->config[&#39;host&#39;], $this->config[&#39;port&#39;]);
 if( ssh2_auth_password($this->conn, $this->config[&#39;username&#39;], $this->config[&#39;password&#39;]))
 {
  
 }else{ 
  echo "无法在服务器进行身份验证";
 }
 
 }
 
 // 传输数据 传输层协议,获得数据
 public function downftp($remote, $local)
 { 
 $ressftp = ssh2_sftp($this->conn);
 return copy("ssh2.sftp://{$ressftp}".$remote, $local);
 }
 
 // 传输数据 传输层协议,写入ftp服务器数据
 public function upftp( $local,$remote, $file_mode = 0777)
 { 
 $ressftp = ssh2_sftp($this->conn);
 return copy($local,"ssh2.sftp://{$ressftp}".$remote); 
 
 }
 
}
Copy after login

The above is the detailed content of PHP SFTP implements upload and download function code strength sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template