PHP实现原生态图片上传封装类方法

2025-05-29 0 49

PHP图片上传类,经典方式,不过上传效率还算可以,我自己用过的一个类,当时对这个类做了些修改,以满足自己特定功能的需要,对PHP熟悉的,可对这个上传类做优化和修改,后附有调用方法,让PHP开发者上传图片轻松容易就做到,先上类代码:

?

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
<?php

class FileUpload_Single

{

//user define -------------------------------------

var $accessPath ;

var $fileSize=200;

var $defineTypeList="jpg|jpeg|gif|bmp";//string jpg|gif|bmp ...

var $filePrefix= "useruplod_";//上传后的文件名前缀,可设置为空

var $changNameMode;//图片改名的规则,暂时只有三类,值范围 : 0 至 2 任一值

var $uploadFile;//array upload file attribute

var $newFileName;

var $error;

function TODO()

{//main 主类:设好参数,可以直接调用

$pass = true ;

if ( ! $this -> GetFileAttri() )

{

$pass = false;

}

if( ! $this -> CheckFileMIMEType() )

{

$pass = false;

$this -> error .= die("<script language=\\"javascript\\">alert('图片类型不正确,允许格式:jpg|jpeg|gif|bmp。');history.back()</script>");

}

if( ! $this -> CheckFileAttri_size() )

{

$pass = false;

$this -> error .= die("<script language=\\"javascript\\">alert('上传的文件太大,请确保在200K以内。');history.back()</script>");

return false;

}

if ( ! $this -> MoveFileToNewPath() )

{

$pass = false;

$this -> error .= die("<script language=\\"javascript\\">alert('上传失败!文件移动发生错误!');history.back()</script>");

}

return $pass;

}

function GetFileAttri()

{

foreach( $_FILES as $tmp )

{

$this -> uploadFile = $tmp;

}

return (empty( $this -> uploadFile[ 'name' ])) ? false : true;

}

function CheckFileAttri_size()

{

if ( ! empty ( $this -> fileSize ))

{

if ( is_numeric( $this -> fileSize ))

{

if ($this -> fileSize > 0)

{

return ($this -> uploadFile[ 'size' ] > $this -> fileSize * 1024) ? false : true ;

}

}

else

{

return false;

}

}

else

{

return false;

}

}

function ChangeFileName ($prefix = NULL , $mode)

{// string $prefix , int $mode

$fullName = (isset($prefix)) ? $prefix."_" : NULL ;

switch ($mode)

{

case 0 : $fullName .= rand( 0 , 100 ). "_" .strtolower(date ("ldSfFYhisa")) ; break;

case 1 : $fullName .= rand( 0 , 100 ). "_" .time(); break;

case 2 : $fullName .= rand( 0 , 10000 ) . time(); break;

default : $fullName .= rand( 0 , 10000 ) . time(); break;

}

return $fullName;

}

function MoveFileToNewPath()

{

$newFileName = NULL;

$newFileName = $this -> ChangeFileName( $this -> filePrefix , 2 ). "." . $this -> GetFileTypeToString();

//检查目录是否存在,不存在则创建,当时我用的时候添加了这个功能,觉得没用的就注释掉吧

/*

$isFile = file_exists( $this -> accessPath);

clearstatcache();

if( ! $isFile && !is_dir($this -> accessPath) )

{

echo $this -> accessPath;

@mkdir($this -> accessPath);

}*/

$array_dir=explode("/",$this -> accessPath);//把多级目录分别放到数组中

for($i=0;$i<count($array_dir);$i++){

$path .= $array_dir[$i]."/";

if(!file_exists($path)){

mkdir($path);

}

}

/////////////////////////////////////////////////////////////////////////////////////////////////

if ( move_uploaded_file( $this -> uploadFile[ 'tmp_name' ] , realpath( $this -> accessPath ) . "/" .$newFileName ) )

{

$this -> newFileName = $newFileName;

return true;

}else{

return false;

}

/////////////////////////////////////////////////////////////////////////////////////////////////

}

function CheckFileExist( $path = NULL)

{

return ($path == NULL) ? false : ((file_exists($path)) ? true : false);

}

function GetFileMIME()

{

return $this->GetFileTypeToString();

}

function CheckFileMIMEType()

{

$pass = false;

$defineTypeList = strtolower( $this ->defineTypeList);

$MIME = strtolower( $this -> GetFileMIME());

if (!empty ($defineTypeList))

{

if (!empty ($MIME))

{

foreach(explode("|",$defineTypeList) as $tmp)

{

if ($tmp == $MIME)

{

$pass = true;

}

}

}

else

{

return false;

}

}

else

{

return false;

}

return $pass;

}

function GetFileTypeToString()

{

if( ! empty( $this -> uploadFile[ 'name' ] ) )

{

return substr( strtolower( $this -> uploadFile[ 'name' ] ) , strlen( $this -> uploadFile[ 'name' ] ) - 3 , 3 );

}

}

}

?>

以下是PHP上传类的调用方法,PHP代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22
<?php

include 'up.class.php';//加载PHP上传类文件

if (empty($HTTP_POST_FILES['image_file']['tmp_name']))//判断接收数据是否为空

{

$tmp = new FileUpload_Single;

$tmp -> accessPath ='upload';//图片上传的目录,这里是当前目录下的upload目录,可自己修改

if ( $tmp -> TODO() )

{

$filename=$tmp -> newFileName;//生成的文件名

echo "图片上传成功,路径为:upload/".$filename;

}else{

echo $tmp -> error;

}

}

else{

echo "没有图片数据可上传";

}

?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP实现原生态图片上传封装类方法 https://www.kuaiidc.com/95860.html

相关文章

发表评论
暂无评论