作者:castellaniygw_623 | 来源:互联网 | 2018-07-16 20:46
ec(2);classUploadimg{ private$_fileName""; 文件域名称如userfile private$_uploadDir; &
class Uploadimg{
private $_fileName=""; //文件域名称 如 'userfile'
private $_uploadDir = ''; //上传路径 如 ./upload/
/*上传参数配置*/
private $_cOnfig= array(
'type'=>array('image/jpeg','image/jpg',
'image/pjpeg','image/gif'), //上传的类型
'size'=>1, //文件最大容量单位是M
'width'=>1000, //图片的最大宽度
'height'=>800 //图片的最大高度
);
/**
* 构造函数
*
* @param string $fileName
* @param string $uploadDir
* @param array $config
*/
function __construct($fileName,$uploadDir,$cOnfig='')
{
$this->_fileName = $fileName;
$this->_uploadDir = $uploadDir;
if($cOnfig== "" or empty($config) or !is_array($config)){
$this->_cOnfig= $this->_config;
}else{
$this->_cOnfig= $config;
}
}
/**
* 检测容量是否超过
*
* @return boolean
*/
function checkSize()
{
if($_FILES[$this->_fileName]['size'] > $this->_config['size']*1024*1024){
return false;
}else{
return true;
}
}
/**
* 获取图片信息
*
* @return boolean
*/
function getInfo()
{
return @getimagesize($_FILES[$this->_fileName]['tmp_name']);
}
/**
* 检测后缀名
*
* @return boolean
*/
function checkExt()
{
$imageInfo = $this->getInfo();
if(in_array($imageInfo['mime'],$this->_config['type'])){
return true;
}else{
return false;
}
}
/**
* 获取后缀名
*
* @return boolean
*/
function getExt()
{
$imageInfo = $this->getInfo();
switch($imageInfo['mime']){
case 'image/jpeg':$filenameExt = '.jpg';break;
case 'image/jpg':$filenameExt = '.jpg';break;
case 'image/pjpeg':$filenameExt = '.jpg';break;
case 'image/gif':$filenameExt = '.gif';break;
default:break;
}
return $filenameExt;
}
/**
* 检测尺寸
*
* @return boolean
*/
function checkWh()
{
$imageInfo = $this->getInfo();
if(($imageInfo[0] > $this->_config['width']) or ($imageInfo[1] > $this->_config['height'])){
return false;
}else{
return true;
}
}
/**
* 上传一张图片
*
* @return string or int
*/
function uploadSingleImage()
{
if($this->checkSize() == false){
return (-3); /*上传容量过大*/
exit();
}
if($this->checkExt() == true){
$filenameExt = $this->getExt();
}else{
return (-2); /*上传格式错误*/
exit();
}
if($this->checkWh() == false){
return (-1); /*上传图片太宽或太高*/
exit();
}
$file_new_name = date('YmdHis').$filenameExt;
$file_new_name_upload = rtrim($_SERVER['DOCUMENT_ROOT'],'/').$this->_uploadDir.$file_new_name;
if(@move_uploaded_file($_FILES[$this->_fileName]['tmp_name'],$file_new_name_upload)){
return $file_new_name;
}else{
return (0); /*上传失败*/
}
}
/**
* 删除图片
*
* @param string $imageName
* @return boolen
*/
function delImage($imageName)
{
$path = rtrim($_SERVER['DOCUMENT_ROOT'],'/').$this->_uploadDir.$imageName;
if(unlink($path) == true){
return true;
}else{
return false;
}
}
}