> 백엔드 개발 > PHP 튜토리얼 > 파일을 업로드하고 썸네일을 생성하는 PHP 함수

파일을 업로드하고 썸네일을 생성하는 PHP 함수

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
풀어 주다: 2016-07-25 09:00:32
원래의
1429명이 탐색했습니다.
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

<?php

/****************************************************

* 返回值:失败:false; 成功:路径.

* UpLoadFileOne(file元素名, 文件夹, 文件类型, 大小).

* 完成一个文件上传功能的函数

* site http://bbs.it-home.org

****************************************************/

function UpLoadFileOne($input, $path='upload', $ftype='gif,jpg,png', $fsize=2){

if(strrpos($path, '/') < strlen($path)-1) $path .= '/';   //上传文件夹.

$Atype = explode(',', $ftype);      //文件类型.

$fsize = $fsize*1048576;            //(1024*1024==1048576=1M)限文件大小,按字节.

$fileInfo = $_FILES[$input];        //文件信息

$name = $fileInfo['name'];          //客户端机器文件的原名称。

$size = $fileInfo['size'];          //上传文件的大小,单位为字节。

$type = $fileInfo['type'];          //上传文件类型.

$tmp_name = $fileInfo['tmp_name']; //文件被上传后在服务端储存的临时文件名。

$error = $fileInfo['error'];        //结果错误信息.

if($error == 0){

     $type = MyFileType($type);      //检测上传文件类型

   $myfile = CreatMyFile($path);   //创建文件夹

   if($myfile==false) return false;

     else $path = $myfile.MakeFname($type);   //文件路径.文件名

   if(in_array($type,$Atype) && $size<=$fsize && is_uploaded_file($fileInfo['tmp_name'])){

     if(@move_uploaded_file($tmp_name, $path)) return str_replace(array('../','./'), '', $path);

     else return false;

     }else return false;

}else return false;

}

/*****************************************************

* 完成多个文件上传功能的函数: http://bbs.it-home.org

* UpLoadFileAll(file元素名,路径,类型,大小)

* <input name='pic[]' id='pic' type='file' size='25'>

* <input name='pic[]' id='pic' type='file' size='25'>

******************************************************/

function UpLoadFileAll($input='UpPic', $path='upload', $ftype='jpg,gif,png', $fsize=2){

$fileInfo = $_FILES[$input];                           //文件信息

if(strrpos($path, '/') < strlen($path)-1) $path .= '/';   //上传文件夹.

$myfile = CreatMyFile($path);                             //创建文件夹

if($myfile==false) return false;

$Atype = explode(',', $ftype);                            //文件类型.

$fsize = $fsize*1048576;                                  //(1024*1024==1048576=1M)限文件大小,按字节.

   $js = "以下文件上传成功:\\n\\n";

if(is_array($fileInfo["error"])){

   foreach ($fileInfo["error"] as $key => $error){

     if ($error == 0) {     

     $name = $fileInfo["name"][$key];              //客户端机器文件的原名称.

     $size = $fileInfo["size"][$key];              //上传文件的大小,单位为字节.

     $type = $fileInfo["type"][$key];              //上传文件类型.

     $tmp_name = $fileInfo["tmp_name"][$key];      //文件被上传后在服务端储存的临时文件名.

     $type = MyFileType($type);                    //检测上传文件类型.

     $path = $myfile.MakeFname($type);             //文件路径包括文件名.    

     if(in_array($type, $Atype) && $size<=$fsize){

     if(@move_uploaded_file($tmp_name, $path)){

      $array[] = $path;

      $js .= " ".$name." 上传成功 !\\n";

        }

     }

     }

   }

}

echo "";

return $array;

}

/*****************************************************************************

* 重设图片尺寸大小:ResizeImage(原图片路径,缩略图(最大)宽度,缩略图(最大)高度)

* 返回值:

*     失败返回: FLASH.

*     成功返回:缩略图路径.

*****************************************************************************/

function ResizeImage($path, $maxwidth, $maxheight){

$picS = substr($path, -3);

$name = substr($path, 0, strrpos($path, '.')).'_S';

switch($picS){

case 'jpg':

    $im = @imagecreatefromjpeg($path);

    break;

case 'gif':

    $im = @imagecreatefromgif($path);

    break;

default:

    $im = @imagecreatefrompng($path);

}

$width = imagesx($im);

$height = imagesy($im);

if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){

   if($maxwidth && $width > $maxwidth){

    $widthratio = $maxwidth/$width;

    $RESIZEWIDTH=true;

    }//end if

    if($maxheight && $height > $maxheight){

     $heightratio = $maxheight/$height;

     $RESIZEHEIGHT=true;

    }//end if

    if($RESIZEWIDTH && $RESIZEHEIGHT){

     if($widthratio < $heightratio){

      $ratio = $widthratio;

     }else{

      $ratio = $heightratio;

     }

    }elseif($RESIZEWIDTH){

     $ratio = $widthratio;

    }elseif($RESIZEHEIGHT){

     $ratio = $heightratio;

    }//end if

    $newwidth = $width * $ratio;

    $newheight = $height * $ratio;

    if(function_exists("imagecopyresampled")){

     $newim = imagecreatetruecolor($newwidth, $newheight);

     imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    }else{

     $newim = imagecreate($newwidth, $newheight);

     imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    }//end if

}else{

   $newim = $im;

}//end if

       switch($picS){

     case 'jpg':

        $PicPath = $name.".jpg";

     if(imagejpeg($newim, $PicPath)){

       imagedestroy($newim);

     return str_replace(array('../','./'), '', $PicPath);

     }else{

     return false;

     }

     break;

     case 'gif':

        $PicPath = $name.".gif";

     if(imagegif($newim, $PicPath)){

       imagedestroy($newim);

     return str_replace(array('../','./'), '', $PicPath);

     }else{

     return false;

     }

     break;

     default:

        $PicPath = $name.".png";

        if(imagepng($newim, $PicPath)){

       imagedestroy($newim);

     return str_replace(array('../','./'), '', $PicPath);

     }else{

     return false;

     }

    }//end switch

}//end function

/**************************

* 文件属性 $type = 文件属性

***************************/

function MyFileType($type) {

    $type = strtolower($type);

switch($type) {

      //OFFICE

   case 'application/msword' :

   $type = 'doc';

   break;

   case 'application/vnd.ms-excel':

   $type = 'xls';

   break;

   case 'application/vnd.ms-powerpoint':

   $type = 'ppt';

   break;

   //压缩

   case 'application/octet-stream':

   $type = 'rar';

   break;

   //文本

   case 'text/plain':

   $type = 'txt';

   break;

   //图片

   case 'image/pjpeg':

   $type = 'jpg';

   break;

   case 'image/gif':

   $type = 'gif';

   break;

   case 'image/x-png':

   $type = 'png';

   break;

   case 'image/bmp':

   $type = 'bmp';

   break;

   default :

   $type = 'err';

}

return $type; //返回文件类型.

}

/******************

* 创建文件夹(路径)

*******************/

function CreatMyFile($fname=''){

switch($fname){

   case '':

    break;

   default:

    if(strrpos($fname, '/') < strlen($fname)-1) $fname .= '/';

}

$fname .= date("Y-m");

if(is_dir($fname)) return $fname.'/';

   if(mkdir($fname, 0755)==false) return false;

//if(chmod($fname, 0777)==false) return false;

return $fname.'/';

}

/*****************************

* 生成文件名

* $fname ==> 文件名称

* $ftype   ==> 文件类型

*****************************/

function MakeFname($ftype) { 

$fname = date("mdHis").'_'.rand(100000, 999999);

return $fname.'.'.$ftype;

}

?>

로그인 후 복사


본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿