1 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2 <?php
3 /**
4 * php文件上传类
5 * @file uploadclass.php
6 * @link http://www.xinui.com
7 * @author kamon <[email protected]>
8 * @copyright 2012
9 * @package UploadFile
10 * @version 0.2beta
11 * @LastModify 2010-7-9 22:36:05
12 */
13 class UploadFile {
14 public $Path;
15 public $UploadDir;
16 public $SaveName;
17 public $FileType;
18 public $MaxSize;
19 public $Err;
20 public $Uploaded;
21 public function __construct(){
22 $this->Path = '../';
23 $this->UploadDir = 'images/';
24 $this->SaveName = '';
25 $this->FileType = array('jpg','gif','bmp','png');
26 $this->MaxSize = 2048;
27 $this->Err = array();
28 $this->Uploaded = false;
29 }
30
31 public function Upload($upfile, $oldfile='',$is_create_dir=false){
32 if(is_uploaded_file($upfile['tmp_name'])){
33 if(!empty($upfile)){
34 if(substr($this->UploadDir,-1,1)!='/' && !empty($this->UploadDir)){
35 $this->UploadDir .= '/';
36 }
37 if(trim($this->SaveName)==''){
38 $this->SaveName = date('YmdHis',time()).rand(100,999);
39 }
40 $fileExt = '.'.end(explode('.', $upfile['name']));
41 $this->SaveName .= $fileExt;
42 $savefile = $this->UploadDir.$this->SaveName;
43 $imgType = explode('/',$upfile['type']);
44 if($upfile['size'] == 0){
45 $this->Err[] = '上传失败,文件大小为0字节!';
46 }
47 if(!in_array(strtolower(substr($fileExt,1)),$this->FileType)){
48 $this->Err[] = '不允许上传该类型的文件!('.$fileExt.')';
49 }
50 if($upfile['error'] !=0){
51 $this->Err[] = '未知错误,文件上传失败!';
52 }
53 if($upfile['size']/1024 > $this->MaxSize){
54 $this->Err[] = '文件大小超出上限,只允许上传大小为 '. $this->MaxSize . 'k的文件!';
55 }
56 if(!file_exists($this->Path.$this->UploadDir)){
57 @mkdir($this->Path.$this->UploadDir,0777);//创建文件目录
58 }
59 if(!file_exists($this->Path.$this->UploadDir) && trim($this->Path.$this->UploadDir)!=''){
60 if($is_create_dir){
61 $dir_path = explode('/',$this->UploadDir);
62 $sub_path = $this->Path;
63 foreach($dir_path as $dir){
64 $sub_path .= $dir.'/';
65 // @mkdir($sub_path,0777);
66 }
67 }else{
68
69 $this->Err[] = '上传目录不存在且设置为不允许创建目录!';
70 }
71 }
72 if(!$this->Err){
73 @move_uploaded_file($upfile['tmp_name'],$this->Path.$savefile);
74 if(trim($oldfile)!='' && file_exists($this->Path.$oldfile)){
75 @unlink($this->Path.$oldfile);
76 }
77 $this->Uploaded = true;
78 return $savefile;
79 }else{
80 @unlink($upfile['tmp_name']);
81 }
82 }
83 }
84 return trim($oldfile);
85 }
86 }
87 /**
88 //调用
89 //if(isset($_FILES['file'])){
90 $file = new UploadFile();
91 $file->Path = './../';
92 $file->UploadDir = "product/image/".date('Ymd');
93 $filename = $file->Upload($_FILES['file']);
94 if($file->Uploaded){
95 echo $filename;
96 }else{
97 print_r($file->Err);
98 }
99 }
100 */
101 ?>
请发表评论