PHP using the SFTP tutorial _php tutorial

Source: Internet
Author: User
Tags ftp connection ftp client file transfer protocol ftp protocol ftp transfer

Using the SFTP tutorial in PHP


This article mainly introduces the use of the SFTP Tutorial in PHP, this article explains the FTP protocol introduction, SSH protocol, SFTP protocol and other knowledge, and gives the FTP and SFTP operation class implementation code, the need for friends can refer to the next

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

/**

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);

http://www.bkjia.com/PHPjc/976543.html www.bkjia.com true http://www.bkjia.com/PHPjc/976543.html techarticle using the SFTP Tutorial in PHP This article mainly introduces the use of the SFTP Tutorial in PHP, this article explains the FTP protocol introduction, SSH protocol, SFTP protocol and other knowledge, and gives the FTP and SFTP operation class implementation code ...

  • 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.