/** Tutorial on using SFTP in PHP Telnet, FTP, SSH, SFTP, SSL (i) Introduction to FTP protocol FTP (file Transfer Protocol, Files Transfer Protocol) is one of the most commonly used protocols on the Internet, and people use FTP to realize the file transfer on the interconnection network. Like many other communication protocols, the FTP protocol also uses the client/server (Client/server) architecture. Users can use a variety of different FTP client programs, With FTP protocol, to connect FTP server to upload or download files FTP command transmission and data transmission is transmitted through different ports FTP is a specific application of TCP/IP, which works on the seventh layer of the OSI model, on the fourth layer of the TCP model, the application layer, using TCP instead of UDP. This allows the FTP client to go through a well-known "three handshake" process before establishing a connection with the server, meaning that the connection between the client and the server is reliable, And it is connection-oriented, which provides a reliable guarantee for the transmission of data. (ii) SSH protocol SSH is all called Secureshell, can report all the transmission data awakened encryption, so that the ' middleman ' will not be able to obtain the data we transmit Colleague, the transmitted data is compressed and can speed up the transfer. SSH has many features that can be replaced by telnet or ftppop, providing a secure channel The most important part of the SSH protocol Framework is three protocols: * The Transport Layer Protocol (the Transport layer Protocol) provides server authentication, data confidentiality, information integrity and other support; * User authentication Protocol (authentication Protocol) provides client identification for the server; * The connection protocol (the Connection Protocol) re-uses the encrypted information tunnel as a plurality of logical channels for use in higher-level application protocols; Various high-level application protocols can be relatively independent of the SSH basic system, and rely on this basic framework, through the connection protocol using SSH security mechanism. (iii) SFTP protocol The protocol for FTP transfer using the SSH protocol is called SFTP (secure file transfer) both SFTP and FTP are file transfer protocols. Difference: SFTP is an SSH-embedded protocol (SSH is an encrypted Telnet protocol), As long as the sshd server is up, it is available, and SFTP security is high, and it does not require an FTP server to start itself. SFTP = SSH + FTP (secure file Transfer Protocol). Because FTP is transmitted in plaintext, Without security, while SFTP is SSH-based, the transmitted content is encrypted and more secure. At present the network is not very safe, used telnet to use SSH2 (SSH1 has been cracked). SFTP this tool and FTP with The same way. However, its transfer files are encrypted by SSL, even if intercepted and can not be cracked. and sftp more than the FTP function, some of the file properties of the settings */ Note here just to introduce FTP, and did not do validation; Class ftp{ Initial configuration is null Private $config =null; Connection is null Private $conn = NULL; Public function init ($config) { $this->config = $config; } FTP connection Public Function connect () { return $this->conn = ftp_connect ($this->config[' host '), $this->config[' Port ']); } Transmit Data Transfer layer protocol, get true or False Public function Download ($remote, $local, $mode = ' auto ') { return $result = @ftp_get ($this->conn, $localpath, $remotepath, $mode); } Transmit Data Transfer layer protocol, upload true or False Public function upload ($remote, $local, $mode = ' auto ') { return $result = @ftp_put ($this->conn, $localpath, $remotepath, $mode); } deleting files Public function Remove ($remote) { return $result = @ftp_delete ($this->conn_id, $file); } } Use $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 '); /* Write an SSH-based FTP class based on the three protocols above We know that there are two ways of identity authentication: public key, password; (1) Login with password (2) password-free login is the use of public key login */ Class sftp{ Initial configuration is null Private $config =null; Connection is null Private $conn = NULL; Whether to log in using the secret key Private $use _pubkey_file= false; Initialization Public function init ($config) { $this->config = $config; } Connect to SSH, there are two ways to connect (1) Use the password (2) using the secret key Public Function connect () { $methods [' hostkey '] = $use _pubkey_file? ' Ssh-rsa ': []; $con = Ssh2_connect ($this->config[' host '), $this->config[' Port '], $methods); (1) When using the secret key if ($use _pubkey_file) { User Authentication Protocol $RC = Ssh2_auth_pubkey_file ( $conn, $this->config[' user '], $this->config[' Pubkey_file '), $this->config[' Privkey_file '), $this->config[' passphrase ']) ); (2) Use login user name and login password }else{ $RC = Ssh2_auth_password ($conn, $this->conf_[' user '], $this->conf_[' passwd '); } return $RC; } Transmit data Transfer layer protocol to obtain Public function Download ($remote, $local) { Return Ssh2_scp_recv ($this->conn_, $remote, $local); } Transport data Transfer layer protocol, write to FTP server Public function upload ($remote, $local, $file _mode=0664) { Return Ssh2_scp_send ($this->conn_, $local, $remote, $file _mode); } deleting files Public function Remove ($remote) { $sftp = ssh2_sftp ($this->conn_); $RC = false; if (Is_dir ("ssh2.sftp://{$sftp}/{$remote}")) { $RC = false; SSH Delete Folder $RC = Ssh2_sftp_rmdir ($sftp, $remote); } else { deleting files $RC = Ssh2_sftp_unlink ($sftp, $remote); } return $RC; } } $config = [ "Host" = "192.168.1.1",//FTP address "User" = "* *", "Port" = "22", "Pubkey_path" = "/root/.ssh/id_rsa.pub",//The storage address of the public key "Privkey_path" = "/root/.ssh/id_rsa",//The storage address of the private key ]; $handle = new Sftpaccess (); $handle->init ($config); $RC = $handle->connect (); $handle->getdata (remote, $local); |