Sftp tutorial in php

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

Sftp tutorial in php

This article mainly introduces the sftp tutorial in php. This article describes ftp protocol introduction, ssh protocol, sftp protocol and other knowledge, and provides the FTP and SFTP operation class implementation code, for more information, see

 

 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

<? Php

 

 

/**

Php sftp tutorial

Telnet, FTP, SSH, SFTP, SSL

(1) ftp protocol Introduction

 

File Transfer Protocol (FTP) is one of the common protocols on the Internet.

As with many other communication protocols, FTP communication protocols also adopt the Client/Server architecture. You can use different FTP client programs,

The FTP protocol is used to connect to the FTP server. The above command transmission and data transmission for transferring or downloading file FTP are transmitted through different ports.

FTP is a specific application of TCP/IP. It works on the Seventh Layer of the OSI model, the fourth layer of the TCP model, that is, the application layer. It uses TCP transmission instead of UDP,

In this way, the FTP client will go through a well-known "three-way handshake" process before establishing a connection with the kimono. The significance of this process is that the connection between the client and the server is reliable,

It is connection-oriented and provides reliable guarantee for data transmission.

 

(2) ssh protocol

 

Ssh is fully called SecureShell, which can report all transmission data to wake up encryption, so that the "man-in-the-middle" cannot obtain the data we transmit.

My colleague, the transmitted data is compressed, which can accelerate the transmission speed. ssh has many functions. It can replace telnet or ftppop and provide a secure channel.

 

The most important part of the SSH protocol framework is the three protocols:

 

* The Transport Layer Protocol supports Server Authentication, data confidentiality, and information integrity;

* The User Authentication Protocol provides The client identity Authentication for The server;

* The Connection Protocol reuses encrypted information tunnels 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) sftp protocol

The protocol used for FTP transmission over SSH is SFTP (Secure File Transfer). Both Sftp and Ftp are file transfer protocols. Difference: sftp is the Protocol included in ssh (ssh is the 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 plain text,

No security. sftp is based on ssh, And the transmitted content is encrypted and secure. Currently, the network is not safe. Previously, all telnet users used ssh2 (SSH1 has been cracked ). Sftp tool and ftp

Same method. However, its transmission file is encrypted through ssl, and cannot be cracked even if it is intercepted. Sftp has more functions than ftp, and more file attribute settings.

 

 

*/

 

 

 

 

// Note that ftp is not verified;

Class ftp {

 

// The initial configuration is NULL.

Private $ config = NULL;

// The 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 the data transport layer protocol to obtain true or false data

Public function download ($ remote, $ local, $ mode = 'auto '){

Return $ result = @ ftp_get ($ this-> conn, $ localpath, $ remotepath, $ mode );

}

 

// Data transmission layer protocol, which uploads data true or false

Public function upload ($ remote, $ local, $ mode = 'auto '){

Return $ result = @ ftp_put ($ this-> conn, $ localpath, $ remotepath, $ mode );

}

 

 

// Delete an object

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 above three Protocols

We know that there are two authentication methods: public key and password;

(1) login with a password

(2) password-free login, that is, using the public key to log on

 

*/

 

Class sftp {

 

 

// The initial configuration is NULL.

Private $ config = NULL;

// The connection is NULL.

Private $ conn = NULL;

 

 

// Whether to log on with the 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) use the 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 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 the login username and password

} Else {

$ Rc = ssh2_auth_password ($ conn, $ this-> conf _ ['user'], $ this-> conf _ ['passwd']);

 

}

 

Return $ rc;

}

 

 

// Transmit the data transport layer protocol to obtain data

Public function download ($ remote, $ local ){

 

Return ssh2_scp_recv ($ this-> conn _, $ remote, $ local );

}

 

// Data transmission layer protocol, which writes data to the ftp server

Public function upload ($ remote, $ local, $ file_mode = 0664 ){

Return ssh2_scp_send ($ this-> conn _, $ local, $ remote, $ file_mode );

 

}

 

// Delete an object

Public function remove ($ remote ){

$ Sftp = ssh2_sftp ($ this-> conn _);

$ Rc = false;

 

If (is_dir ("ssh2.sftp: // {$ sftp}/{$ remote }")){

$ Rc = false;

 

// Delete a folder through ssh

$ Rc = ssh2_sftp_rmdir ($ sftp, $ remote );

} Else {

// Delete an object

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

 

Related Article

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.