php中使用sftp教程_PHP教程

來源:互聯網
上載者:User

php中使用sftp教程


這篇文章主要介紹了php中使用sftp教程,本文講解了ftp 協議簡介、ssh協議、sftp 協議等知識,並給出了FTP和SFTP操作類實現代碼,需要的朋友可以參考下

?

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 中的sftp 使用教程

Telnet、FTP、SSH、SFTP、SSL

(一) ftp 協議簡介

FTP(File Transfer Protocol,檔案傳輸通訊協定)是互連網上常用的協議之一,人們用FTP實現互連網上的檔案傳輸。

如同其他的很多通訊協議,FTP通訊協議也採用客戶機 / 伺服器(Client / Server )架構。使用者可以通過各種不同的FTP用戶端程式,

藉助FTP協議,來串連FTP伺服器,以上傳或者下載檔案FTP的命令傳輸和資料轉送是通過不同的連接埠進行傳輸的

FTP是TCP/IP的一種具體應用,它工作在OSI模型的第七層,TCP模型的第四層上,即應用程式層,使用TCP傳輸而不是UDP,

這樣FTP客戶在和服 務器建立串連前就要經過一個被廣為熟知的"三向交握"的過程,它帶來的意義在於客戶與伺服器之間的串連是可靠的,

而且是連線導向,為資料的傳輸提供了可靠 的保證。

(二)ssh協議

ssh 的全稱為 SecureShell ,可以報所有的傳輸資料驚醒加密,這樣'中間人'就不能獲得我們傳輸的資料

同事,傳輸的資料是經過壓縮的,可以加快傳輸的速度.ssh有很多功能,可以替代telnet 也可也為ftppop ,提供一個安全的通道

SSH協議架構中最主要的部分是三個協議:

* 傳輸層協議(The Transport Layer Protocol)提供伺服器認證,資料機密性,資訊完整性 等的支援;

* 使用者認證協議(The User Authentication Protocol) 則為伺服器提供用戶端的身份鑒別;

* 連線協定(The Connection Protocol) 將加密的資訊隧道複用成若干個邏輯通道,提供給更高層的應用協議使用;

各種高層應用協議可以相對地獨立於SSH基本體系之外,並依靠這個基本架構,通過連線協定使用SSH的安全機制。

(三)sftp 協議

使用SSH協議進行FTP傳輸的協議叫SFTP(安全檔案傳輸)Sftp和Ftp都是檔案傳輸通訊協定。區別:sftp是ssh內含的協議(ssh是加密的telnet協議),

只要sshd伺服器啟動了,它就可用,而且sftp安全性較高,它本身不需要ftp伺服器啟動。 sftp = ssh + ftp(安全檔案傳輸通訊協定)。由於ftp是明文傳輸的,

沒有安全性,而sftp基於ssh,傳輸內容是加密過的,較為安全。目前網路不太安全,以前用telnet的都改用ssh2(SSH1已被破解)。sftp這個工具和ftp用

法一樣。但是它的傳輸檔案是通過ssl加密了的,即使被截獲了也無法破解。而且sftp相比ftp功能要多一些,多了一些檔案屬性的設定

*/

// 注意這裡只是為了介紹ftp ,並沒有做驗證 ;

class ftp{

// 初始配置為NULL

private $config =NULL ;

// 串連為NULL

private $conn = NULL;

public function init($config){

$this->config = $config;

}

// ftp 串連

public function connect(){

return $this->conn = ftp_connect($this->config['host'],$this->config['port']));

}

// 傳輸資料 傳輸層協議,獲得資料 true or false

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

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

}

// 傳輸資料 傳輸層協議,上傳資料 true or false

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

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

}

// 刪除檔案

public function remove($remote){

return $result = @ftp_delete($this->conn_id, $file);

}

}

// 使用

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

/*根據上面的三個協議寫出基於ssh 的ftp 類

我們知道進行身份認證的方式有兩種:公開金鑰;密碼 ;

(1) 使用密碼登陸

(2) 免密碼登陸也就是使用公開金鑰登陸

*/

class sftp{

// 初始配置為NULL

private $config =NULL ;

// 串連為NULL

private $conn = NULL;

// 是否使用秘鑰登陸

private $use_pubkey_file= false;

// 初始化

public function init($config){

$this->config = $config ;

}

// 串連ssh ,串連有兩種方式(1) 使用密碼

// (2) 使用秘鑰

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

// 使用者認證協議

$rc = ssh2_auth_pubkey_file(

$conn,

$this->config['user'],

$this->config['pubkey_file'],

$this->config['privkey_file'],

$this->config['passphrase'])

);

//(2) 使用登陸使用者名稱字和登陸密碼

}else{

$rc = ssh2_auth_password( $conn, $this->conf_['user'],$this->conf_['passwd']);

}

return $rc ;

}

// 傳輸資料 傳輸層協議,獲得資料

public function download($remote, $local){

return ssh2_scp_recv($this->conn_, $remote, $local);

}

//傳輸資料 傳輸層協議,寫入ftp伺服器資料

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

return ssh2_scp_send($this->conn_, $local, $remote, $file_mode);

}

// 刪除檔案

public function remove($remote){

$sftp = ssh2_sftp($this->conn_);

$rc = false;

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

$rc = false ;

// ssh 刪除檔案夾

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

} else {

// 刪除檔案

$rc = ssh2_sftp_unlink($sftp, $remote);

}

return $rc;

}

}

$config = [

"host" => "192.168.1.1 ", // ftp地址

"user" => "***",

"port" => "22",

"pubkey_path" => "/root/.ssh/id_rsa.pub", // 公開金鑰的儲存地址

"privkey_path" => "/root/.ssh/id_rsa", // 私密金鑰的儲存地址

];

$handle = new SftpAccess();

$handle->init($config);

$rc = $handle->connect();

$handle->getData(remote, $local);

http://www.bkjia.com/PHPjc/976543.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/976543.htmlTechArticlephp中使用sftp教程 這篇文章主要介紹了php中使用sftp教程,本文講解了ftp 協議簡介、ssh協議、sftp 協議等知識,並給出了FTP和SFTP操作類實現代碼...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.