天天看點

PHP的ftp檔案,多檔案上傳操作類

原文部落格位址http://www.xiegaosheng.com/post/view?id=97;

PHP針對ftp檔案的操作方法,如果是隻操作一個ftp,可以使用裡面的單利模式,

不需要每次都去執行個體化,我的項目中需要去連結很多個ftp伺服器;

是以需要多次去連接配接和關閉;

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

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

<?php

namespace

App\Tools;

class

FtpFile

{

static

private

$_instance

=null;

private

$ftp

= null;

public

$off

;             

// 傳回操作狀态(成功/失敗)

//私有的構造方法

public

function

__construct(

$config

){

//執行個體化

$this

->ftp = @ftp_connect(

$config

[

'ftp_ip'

],

$config

[

'ftp_port'

]) 

or

die

(

"FTP connection fail"

);

//登入驗證

@ftp_login(

$this

->ftp,

$config

[

'ftp_username'

],

$config

[

'ftp_password'

]);

//是否開啟被動模式

if

(isset(

$config

[

'ftp_pasv'

]))

{

@ftp_pasv(

$this

->ftp,true);

}

}

static

public

function

getInstance(

$config

){

if

(!(self::

$_instance

instanceof

self)){

self::

$_instance

new

FtpFile(

$config

);

}

return

self::

$_instance

;

}

function

up_file(

$path

,

$newpath

,

$type

=true)

{

if

(

$type

$this

->dir_mkdirs(

$newpath

);

$this

->off = @ftp_put(

$this

->ftp,

$newpath

,

$path

,FTP_BINARY);

if

(!

$this

->off)

{

return

"檔案上傳失敗,請檢查權限及路徑是否正确!"

;

}

else

{

//删除檔案

unlink(

$path

);

return

true;

}

}

public

function

uploadFile(

$files

=[],

$type

=true)

{

if

(

is_array

(

$files

))

{

foreach

(

$files

as

$key

=>

$file

)

{

if

(

$type

)

{

$this

->dir_mkdirs(

$file

);

}

$this

->off = @ftp_put(

$this

->ftp,

$file

,

$key

,FTP_BINARY);

if

(!

$this

->off)

{

logs(

'ftp.txt'

,

date

(

'Y-m-d H:i:s'

).

$file

.

"檔案上傳錯誤"

);

}

else

{

//删除檔案

unlink(

$key

);

// return true;

}

}

}

if

(!

$this

->off)

{

//logs函數自定義日志

logs(

'ftp.txt'

,

date

(

'Y-m-d H:i:s'

).

$file

.

"檔案上傳錯誤"

);

return

false;

}

else

{

return

true;

}

}

function

move_file(

$path

,

$newpath

,

$type

=true)

{

if

(

$type

$this

->dir_mkdirs(

$newpath

);

$this

->off = @ftp_rename(

$this

->ftp,

$path

,

$newpath

);

if

(!

$this

->off) {

return

"檔案移動失敗,請檢查權限及原路徑是否正确!"

;

}

else

{

return

true;

}

}

function

copy_file(

$path

,

$newpath

,

$type

=true)

{

$downpath

"/var/www/temp.txt"

;

$this

->off = @ftp_get(

$this

->ftp,

$downpath

,

$path

,FTP_BINARY);

// 下載下傳 

if

(!

$this

->off) 

{

return

"檔案複制失敗,請檢查權限及原路徑是否正确!"

;

}

$this

->up_file(

$downpath

,

$newpath

,

$type

);

}

function

del_file(

$path

)

{

$this

->off = @ftp_delete(

$this

->ftp,

$path

);

if

(!

$this

->off){

return

false;

}

}

function

dir_mkdirs(

$path

)

{

$path_arr

explode

(

'/'

,

$path

);       

// 取目錄數組 

$file_name

array_pop

(

$path_arr

);      

// 彈出檔案名

$path_div

count

(

$path_arr

);        

// 取層數 

foreach

(

$path_arr

as

$val

)          

// 建立目錄 

{

if

(@ftp_chdir(

$this

->ftp,

$val

) == FALSE)

{

$tmp

= @ftp_mkdir(

$this

->ftp,

$val

);

if

(

$tmp

== FALSE)

{

exit

;

}

@ftp_chdir(

$this

->ftp,

$val

);

}

}

for

(

$i

=1;

$i

<=

$path_div

;

$i

++)         

// 回退到根 

{

@ftp_cdup(

$this

->ftp);

}

}

public

function

close()

{

@ftp_close(

$this

->ftp);

}

public

function

__destruct()

{

// TODO: Implement __destruct() method.

//@ftp_close($this->ftp);

}

}

lavarel中直接調用;

單例模式調用:

1

FtpFile::getInstance(

$this

->data)->up_file(

$location_file

,

$remote_file

);

不是單例模式調用;

1

2

3

$ftp

new

FtpFile(

$this

->data);

$ftp

->uploadFile(

$filearr

);

$ftp

->close();