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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
| define('WEB_ROOT', dirname(__FILE__));
/*
* image upload class
* $maxsize int 允许上传的最大文件 单位:K
* $file 上传的文件域 $_FILES['pic']
* $errorlog true/false 是否记录上传错误日志
* $Aextend 允许上传的后缀名
* $path 上传文件保存的路径
* $tpath 缩略图保存的路径
* $twidth 缩略图的宽度
* $theight 缩略图的高度
* $thumb 是否生成缩略图
* $newname 上传后文件的保存名
* $thumbname 缩略文件名 完整路径
* $picname 图片保存的完整路径
* 例子:$up = new Upload($_FILES['pic']);
* $up->DoUpload();
*/
class Upload{
private $maxsize;
private $file;
private $errorlog ;
private $Aextend = array('jpg','gif','png');
private $path;
private $tpath;
private $twidth;
private $theight;
private $thumb;
private $newname;
private $thumbname = '';
private $picname;
function __construct($file,$thumb = true,$twidth = 100,$theight = 100,$tpath = './thumb000000/',$path = './upload000000/',$maxsize = 10240000,$errorlog = true){
$this->maxsize = $maxsize;
$this->file = $file;
$this->errorlog = $errorlog;
$this->path = $path;
$this->tpath = $tpath;
$this->thumb = $thumb;
$this->twidth = $twidth;
$this->theight = $theight;
}
private function GetExtend(){
$type = $this->file['type'];
$extend = '';
switch($type){
case 'image/pjpeg' :
case 'image/jpeg' :
$extend = "jpg";
break;
case 'image/gif' :
$extend = "gif";
break;
case 'image/png' :
$extend = "png";
break;
}
return $extend;
}
private function GetSize(){
return $this->file['size']/1024;
}
private function Mkdirs($p){
return mkdir($p,0777)&&file_put_contents($p.'/index.htm',' ');
}
private function GetNewName(){
return $this->newname = 'duizhang_'.time().'.'.$this->GetExtend();
}
//private function GetThumbName(){
// return $this->thumbname = $this->tpath.'duizhang_'.time().'.'.$this->GetExtend();
//}
/*
* 生成缩略图和原始图目录
* 如原始图目录:./upload000000/2011/06/11/
*/
private function DoMkdirs(){
$year = date('Y',time()+8*60*60);
$month = date('m',time()+8*60*60);
$day = date('d',time()+8*60*60);
try{
if(!is_dir($this->path)){
if(!$this->Mkdirs($this->path)) throw new FileException('上传文件',2);
}
if(!is_dir($this->path.$year)){
if(!$this->Mkdirs($this->path.$year)) throw new FileException('上传文件',2);
}
if(!is_dir($this->path.$year.'/'.$month)){
if(!$this->Mkdirs($this->path.$year.'/'.$month)) throw new FileException('上传文件',2);
}
if(!is_dir($this->path.$year.'/'.$month.'/'.$day)){
if(!$this->Mkdirs($this->path.$year.'/'.$month.'/'.$day)) throw new FileException('上传文件',2);
}
$this->path = $this->path.$year.'/'.$month.'/'.$day.'/';
if($this->thumb === true){
if(!is_dir($this->tpath)){
if(!$this->Mkdirs($this->tpath)) throw new FileException('上传文件',2);
}
if(!is_dir($this->tpath.$year)){
if(!$this->Mkdirs($this->tpath.$year)) throw new FileException('上传文件',2);
}
if(!is_dir($this->tpath.$year.'/'.$month)){
if(!$this->Mkdirs($this->tpath.$year.'/'.$month)) throw new FileException('上传文件',2);
}
if(!is_dir($this->tpath.$year.'/'.$month.'/'.$day)){
if(!$this->Mkdirs($this->tpath.$year.'/'.$month.'/'.$day)) throw new FileException('上传文件',2);
}
$this->tpath = $this->tpath.$year.'/'.$month.'/'.$day.'/';
}
}catch(FileException $e){
$errormessage = $e->Getmessage().'---'.$e->GetDetail();
echo $errormessage;
if($this->errorlog){
DoLog::Instance('errorupload');
DoLog::WriteLog($errormessage);
}
}
}
//执行上传
function DoUpload(){
$this->DoMkdirs();
try{
if(!in_array($this->GetExtend(),$this->Aextend)) throw new FileException('上传文件',0);
if($this->GetSize() > $this->maxsize || $this->GetSize()<=0) throw new FileException('上传文件',1);
if(!is_writable($this->path)) throw new FileException('上传文件',7);
if(!is_writable($this->tpath)) throw new FileException('上传文件',7);
if(!move_uploaded_file($this->file['tmp_name'],$this->path.$this->GetNewName())) throw new FileException('上传文件',8);
$this->picname = $this->path.$this->newname;
if($this->thumb === true){
if(!$this->ThumbImg($this->twidth,$this->theight,$this->path.$this->newname,$this->tpath.$this->newname,$this->GetExtend())) throw new FileException('上传文件',9);
$this->thumbname = $this->tpath.$this->newname;
}
}catch(FileException $e){
$errormessage = $e->Getmessage().'---'.$e->GetDetail();
echo $errormessage;
if($this->errorlog){
DoLog::Instance('errorupload');
DoLog::WriteLog($errormessage);
}
}
}
/**
*
* 图片缩略方法
* @param int $twidth 图片宽度
* @param int 图片长度
* @param string $patch 原图片保存路径
* @param string $tpath 缩略图保存路径
* @param string $type 要缩略的图片的类型(后缀)
*/
private function ThumbImg($twidth,$theight,$patch,$tpath,$type){
switch ($type)
{
case "jpg":
$image2=imagecreatefromjpeg($patch);
function outputimg($image,$path){imagejpeg($image,$path);};
break;
case "gif":
$image2=imagecreatefromgif($patch);
function outputimg($image,$path){imagegif($image,$path);};
break;
case "png":
$image2=imagecreatefrompng($patch);
function outputimg($image,$path){imagepng($image,$path);};
break;
}
$image=imagecreatetruecolor($twidth, $theight);
if($type=="png"){
imagesavealpha($image2,true);//设置png透明alpha通道开启
imagealphablending($image,false);//不合并alpha通道图层
imagesavealpha($image,true);
}
$image2x=imagesx($image2);
$image2y=imagesy($image2);
imagecopyresampled($image, $image2, 0, 0, 0, 0, $twidth, $theight, $image2x, $image2y);
outputimg($image,$tpath);
imagedestroy($image2);
imagedestroy($image);
return true;
}
function PicName(){
return $this->picname;
}
function ThumbName(){
return $this->thumbname;
}
}
//自定义文件异常处理类
class FileException extends Exception{
function GetDetail(){
switch($this->code){
case 0:
return $this->GetTime()."\t".'上传文件类型错误';
break;
case 1:
return $this->GetTime()."\t".'上传文件大小超过限制';
break;
case 2:
return $this->GetTime()."\t".'创建上传目录失败';
break;
case 3:
return $this->GetTime()."\t".'创建日志记录目录失败';
break;
case 4:
return $this->GetTime()."\t".'创建日志文件失败';
break;
case 5:
return $this->GetTime()."\t".'打开日志文件失败';
break;
case 6:
return $this->GetTime()."\t".'写入日志文件失败';
break;
case 7:
return $this->GetTime()."\t".'上传目录无写入权限';
break;
case 8:
return $this->GetTime()."\t".'上传文件失败';
break;
case 9:
return $this->GetTime()."\t".'缩略图生成失败';
break;
default:
return $this->GetTime()."\t".'当前操作出现异常';
break;
}
}
function GetTime(){
return date('Y-m-d H:i:s',time()+8*60*60);
}
}
///记录错误日志
//如:上传文件错误日志存放在根目录下/log/errorupload/20110610.txt
class DoLog{
static private $dir = './log';
static private $path;
static private $message = '日志记录';
private function __construct(){}
/*
*日志类 初始化方法
*$type 记录日志的类型 如:上传错误日志 类型为 errorupload
*/
static public function Instance($type){
if(!is_dir(self::$dir)) self::Mkdirs(self::$dir);
if(!is_dir(self::$dir.'/'.$type)) self::Mkdirs(self::$dir.'/'.$type);
self::$path = self::$dir.'/'.$type.'/'.date('Y-m-d',time()+8*60*60).'.txt';
if(!file_exists(self::$path)) self::Touch();
}
/*创建日志目录*/
static private function Mkdirs($d){
try{
if(!mkdir($d,0777)) throw new FileException(self::$message.'---'.$d,3);
}catch(FileException $e){
echo $e->Getmessage().'===='.$e->GetDetail();
}
}
/*创建日志文件*/
static private function Touch(){
try{
if(!touch(self::$path)) throw new FileException(self::$path,4);
}catch(FileException $e){
echo $e->Getmessage().'===='.$e->GetDetail();
}
}
/*
*讲错误日志写入文件
*$m 错误日志信息
*/
static public function WriteLog($m){
try{
if(!$fp = fopen(self::$path,'a+')) throw new FileException(self::$path,5);
if(!fwrite($fp,$m."\r\n")) throw new FileException(self::$path,6);
fclose($fp);
}catch(FileException $e){
echo $e->Getmessage().'===='.$e->GetDetail();
}
}
} |