Php sftp tutorial, php sftp tutorial

Source: Internet
Author: User
Tags ftp client

Php sftp tutorial, php sftp tutorial

<? Php/** sftp usage tutorial in php Telnet, FTP, SSH, SFTP, SSL (1) ftp Protocol introduction FTP (File Transfer Protocol, File Transfer Protocol) it is one of the common protocols on the Internet. People use FTP to implement file transmission over the Internet. As with many other communication protocols, FTP communication protocols also adopt the Client/Server architecture. You can use different FTP client programs to connect to the FTP server through the FTP protocol, the command transmission and data transmission for uploading or downloading files over FTP are performed over different ports. FTP is a specific application of TCP/IP, which works on the Seventh Layer of the OSI model, the layer-4 layer of the TCP model, that is, the application layer, uses TCP transmission instead of UDP, so that the FTP client will go through a well-known "three-way handshake" process before establishing a connection with the kimono, it makes sense that the connection between the customer and the server is reliable and connection-oriented, providing a reliable guarantee for data transmission. (2) The ssh protocol ssh is fully called SecureShell. It can report all transmission data to wake up encryption, so that the "man-in-the-middle" cannot obtain the data transmitted by our colleagues, the transmitted data is compressed to speed up transmission. ssh has many functions. It can replace telnet or ftppop. The most important part of a secure channel SSH protocol framework is three protocols: * The Transport Layer Protocol supports Server Authentication, data confidentiality, and information integrity. * The User Authentication Protocol) the Connection Protocol reuses The encrypted information tunnel into several logical channels for higher-level application protocols; various high-level application protocols can be relatively independent from the SSH basic system, and rely on this basic framework to use SSH security mechanisms through connection protocols. (3) the sftp protocol uses the SSH protocol for FTP transmission. SFTP (Secure File Transfer) Sftp and Ftp are both file transfer protocols. Difference: sftp is the Protocol included in ssh (ssh is an encrypted telnet protocol). As long as the sshd server is started, it is available, and sftp is highly secure, it does not need to be started on the ftp server. Sftp = ssh + ftp (Secure File Transfer Protocol ). Because ftp is transmitted in plaintext without security, while sftp is based on ssh, the transmitted content is encrypted and secure. Currently, the network is not safe. Previously, all telnet users used ssh2 (SSH1 has been cracked ). Sftp is the same as ftp. However, its transmission file is encrypted through ssl, and cannot be cracked even if it is intercepted. In addition, sftp has more functions than ftp, and some file attribute settings are added * // note that this is only to introduce ftp and verification is not performed; class ftp {// The initial configuration is NULLprivate $ config = NULL; // The connection is NULL private $ conn = NULL; public function init ($ config) {$ this-> config = $ config;} // ftp connects to public function connect () {return $ this-> conn = ftp_connect ($ this-> config ['host'], $ this-> config ['Port']);} // transmit data transport layer protocol, get data true or false public function download ($ remote, $ local, $ mode = 'auto') {r Eturn $ result = @ ftp_get ($ this-> conn, $ localpath, $ remotepath, $ mode) ;}// transport data transport layer protocol, upload data true or false public function upload ($ remote, $ local, $ mode = 'auto') {return $ result = @ ftp_put ($ this-> conn, $ localpath, $ remotepath, $ mode);} // Delete the 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 preceding three protocols. We know there are two authentication methods: public key and password. (1) login with password (2) password-free login, that is, login with the public key */class sftp {// The initial configuration is NULLprivate $ config = NULL; // The connection is NULL private $ conn = NULL; // whether to use the key to log on to private $ use_pubkey_file = false; // initialize public functio N init ($ config) {$ this-> config = $ config;} // connect to ssh in two ways (1) Use the password // (2) use the public function connect () {$ methods ['hostkey'] = $ use_pubkey_file? 'Ssh-rsa ': []; $ con = ssh2_connect ($ this-> config ['host'], $ this-> config ['Port'], $ methods ); // (1) 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 the login username and login password} else {$ rc = ssh2_auth_password ($ conn, $ this-> conf _ ['user'], $ this-> conf _ ['passwd']);} return $ rc;} // Transfer Data Transport Layer Protocol to obtain the public function download ($ remote, $ local) {return ssh2_scp_recv ($ this-> conn _, $ remote, $ local);} // Transfer Data Transport Layer Protocol, written to the ftp server data public function upload ($ remote, $ local, $ file_mode = 0664) {return ssh2_scp_send ($ this-> conn _, $ local, $ remote, $ file_mode );} // delete the file public function remove ($ remote) {$ sftp = ssh2_sftp ($ this-> conn _); $ rc = false; if (is_dir ("ssh2.sftp: // {$ sftp}/{$ remote} ") {$ rc = false; // delete a folder Using ssh $ rc = ssh2_sftp_rmdir ($ sftp, $ remote );} else {// delete the file $ 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 ", // public key storage address" privkey_path "=>"/root /. ssh/id_rsa ", // Private Key storage address]; $ handle = new SftpAccess (); $ handle-> init ($ config ); $ rc = $ handle-> connect (); $ handle-> getData (remote, $ local );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.