| 
 class upload { 
private $file_name = ‘null’; #客户端文件名 
private $file_size = 0; #文件大小 
private $file_tmpName = ”; #临时文件夹路径 
private $upload_type = array(‘jpg’,'jpeg’,'gif’,'png’); #允许上传的文件类型 
private $upload_Ext = ‘null’; #文件后缀 
private $upload_dir = ‘null’; #新路径 
private $upload_name = ‘null’; #新文件名 
private $upload_width = 0; #图片宽度 
private $upload_height = 0; #图片高度 
private $upload_msg = ”; #上传消息 
private $upload_mode; #上传状态 
/** 
* 构造函数,自动上传 
* @access public 
* @param string $control 文件域控件名 
* @param string $filePath 保存路径 
* @param string $fileName 新文件名,不带后缀 
* @param array $fileType 允许上传的文件后缀 
* @param int $size 允许上传的文件大小,以字节为单位 
*/ 
public function __construct($control=”,$filePath=”,$fileName=”,$fileType=”,$size=0) { 
$this->upload_mode = true; 
if($control==”) 
$this->error(‘上传的文件域控件名不能为空’); 
else{ 
$this->file_name=$_FILES[$control]['name']; 
$this->file_size=$_FILES[$control]['size']; 
$this->file_tmpName=$_FILES[$control]['tmp_name']; 
if($fileType!=”) 
$this->upload_type = $fileType; 
if($filePath==”) 
$this->upload_dir=’./’; 
else 
$this->upload_dir=$filePath; 
if(!is_uploaded_file($this->file_tmpName)) 
$this->error(‘没有文件被上传’); 
else{ 
if($size!=0&&$this->file_size>$size) 
$this->error(‘上传的图片文件不能大于’.$this->getSize($size)); 
if(!$this->typeDetect($this->file_name)) 
$this->error(‘不支持上传此类型的文件’); 
else{ 
if(!$this->fileDetect($this->file_tmpName)) 
$this->error(‘上传的图片文件无效或已经损坏’); 
} 
if($fileName==”) 
$this->upload_name=$this->file_name; 
else 
$this->upload_name=$fileName.’.’.$this->upload_Ext; 
} 
} 
//开始保存文件 
if($this->mode) 
$this->saveFile(); 
} 
/** 
* 获取属性 
* @access public 
* @param string 属性名 
* @return 属性值 
*/ 
public function __get($name){ 
switch($name){ 
case ‘path’: 
return $this->upload_dir; 
break; 
case ‘name’: 
return $this->upload_name; 
break; 
case ‘ext’: 
return $this->upload_Ext; 
break; 
case ‘size’: 
return $this->getSize($this->file_size); 
case ‘width’: 
return $this->upload_width; 
break; 
case ‘height’: 
return $this->upload_height; 
break; 
case ‘mode’: 
return $this->upload_mode; 
break; 
case ‘msg’: 
return $this->upload_msg; 
break; 
} 
} 
/** 
* 检测文件后缀名 
* @access public 
* @return bool 
*/ 
private function typeDetect($OldFile){ 
$tempArr = explode(“.”, $OldFile); 
$fileExt = array_pop($tempArr); 
$fileExt = trim($fileExt); 
$fileExt = strtolower($fileExt); 
if(in_array($fileExt,$this->upload_type)){ 
$this->upload_Ext=$fileExt; 
return true; 
} 
else 
return false; 
} 
/** 
* 检测上传文件是否是有效图片格式 
* @access private 
* @return bool 
*/ 
private function fileDetect($tmpName){ 
$arr = getimagesize($tmpName); 
if(!$arr){ 
return false; 
}else{ 
$this->upload_width = $arr[0]; 
$this->upload_height = $arr[1]; 
return true; 
} 
} 
/** 
* 保存文件 
* @access private 
*/ 
private function saveFile(){ 
if(!is_dir($this->upload_dir)){ 
mkdir($this->upload_dir, 0777); 
chmod($this->upload_dir, 0777); 
} 
$save_file = move_uploaded_file($this->file_tmpName,$this->upload_dir.$this->upload_name); 
if(!$save_file) 
$this->error(‘文件写入失败’); 
else 
$this->upload_msg=’文件上传成功’; 
} 
/** 
* 获取文件大小 
* @return string 返回B,KB,MB单位 
*/ 
private function getSize($tmpSize){ 
$value = ‘B’; 
if($tmpSize>1024){ 
$tmpSize = floor($tmpSize/1024); 
$value = ‘KB’; 
} 
if($tmpSize>1024){ 
$tmpSize = round($tmpSize/1024,2); 
$value = ‘MB’; 
} 
return $tmpSize.$value; 
} 
/** 
* 错误提示 
* @access private 
* @param string $msg 错误信息 
*/ 
private function error($msg=”){ 
$this->upload_msg .= $msg; 
$this->upload_mode = false; 
} 
} 
 |