_php tips for using sftp tutorials in PHP

Source: Internet
Author: User
Tags ftp connection ftp client ssh file transfer protocol ftp protocol
<?php/** php SFTP Use tutorials Telnet, FTP, SSH, SFTP, SSL (i) Introduction to FTP protocol FTP (file Transfer Protocol, document Transfer Protocol) is one of the common protocols on the Internet,
People use FTP to realize the transmission of files on the interconnection network. Like many other communication protocols, the FTP protocol also uses a client/server (Client/server) architecture. Users can through a variety of FTP client programs, with the help of FTP protocol, to connect the FTP server to upload or download files FTP command transmission and data transmission is through different ports for the transmission of FTP is a specific application of TCP/IP, it works in the OSI model of the seventh layer, On the fourth layer of the TCP model, the application tier, which uses TCP transport instead of UDP, so that the FTP client passes through a well-known "three handshake" process prior to establishing a connection to the server, which is meant to be reliable and connection-oriented.

Provides a reliable guarantee for the transmission of data. (ii) SSH protocol ssh is called Secureshell, can report all the transmission data wake up encryption, so that ' middleman ' can not get our data transfer colleagues, the transmission of data is compressed, can speed up the transmission. SSH has many features that can replace Telnet Also available for Ftppop, a secure channel SSH protocol framework is the most important part of the three protocols: * Transport Layer Protocol (the transport Layer Protocol) to provide server authentication, data confidentiality, information integrity and other support; * User authentication Protocol (T The He User authentication Protocol) provides the client with authentication for the server; * The Connection Agreement (the Connection Protocol) uses the encrypted information tunnel as a plurality of logical channels, which is provided to the higher level application protocol; various high-level
 
 Application protocols can be relatively independent of the SSH base system and rely on this basic framework to use the security mechanism of SSH through connection protocols. (iii) The SFTP protocol uses the SSH protocol for FTP transmission of the protocol called SFTP (secure file transfer) SFTP and FTP are file transfer protocols. Difference: SFTP is an SSH-contained protocol (SSH is the encrypted Telnet protocol), and as long as the sshd server is started, it is available and SFTP security is high, and it does not require an FTP server to start itself. SFTP = SSH + FTP (Secure file transferAgreement). Because FTP is plaintext transmission, there is no security, and sftp based on SSH, transmission content is encrypted, more secure. At present, the network is not very safe, used to use Telnet to SSH2 (SSH1 has been cracked). SFTP This tool is the same as FTP usage. But its transmission file is encrypted through SSL, even if it is intercepted can not be cracked. 		
and SFTP compared to the FTP function more, a number of file properties set//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->confi 
	g[' Port ']); ///Transport Data Transfer layer protocol, obtaining the true or false public function download ($remote, $local, $mode = ' auto ') {return $result = @
  Ftp_get ($this->conn, $localpath, $remotepath, $mode); //Transport Data Transfer layer protocol, upload true or false public function upload ($remote, $local, $mode = ' auto ') {return $result = @ft
  P_put ($this->conn, $localpath, $remotepath, $mode);
	//Delete file 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 ');
/* According to the above three protocols to write the FTP based on SSH class we know that there are two ways to authenticate authentication: public key;
	(1) The use of password login (2) password-free landing is the use of public key login/class sftp{//Initial configuration for NULL private $config =null;

	
	Connection is null private $conn = NULL;
	
	Whether use secret key to login private $use _pubkey_file= false; 
	Initializes public function init ($config) {$this->config = $config; //Connect SSH, there are two ways to connect (1) Use password//(2) Use 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; ///Transport Data Transfer layer protocol to obtain the public function download ($remote, $local) {return ssh2_scp_recv ($this->conn_, $re
	  Mote, $local); }//Transport data Transfer layer protocol, written to FTP server upload ($remote, $local, $file _mode=0664) {return ssh2_scp_send ($this-
		  
	 >conn_, $local, $remote, $file _mode);
			//Delete file 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 {//delete file $RC = Ssh2_sftp_unlink ($sftp, $remote);
			
		return $RC; } $config = ["Host" => "192.168.1.1",//FTP address "user" => "* *", "Port" => "2 2 "," 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.