<?php
class Upload
{
private $doc; //文件
private $docsize; //文件大小
private $docname; //文件名字
private $doctype; //文件类型
public $docnewname; //图片新名字
private $allowtype; //可以上传的文件类型
private $seterror=1; //错误原因
private $doctmpname; //文件临时名
private $path; //文件存储路径
function __construct($file,$path)
{
$this->doc=$file;
$this->path=$path;
$this->docsize=$file[‘size‘];
$this->docname=$file[‘name‘];
$this->doctmpname=$file[‘tmp_name‘];
$array=explode(‘.‘,$this->docname);
$allowtype=Array("jpg","jpeg","gif","png","bmp");
$this->doctype=$array[count($array)-1];
$this->docnewname=time().".".$this->doctype;
$error=$file[‘error‘];
if($error!=0)
{
switch($error)
{
case 1:$this->seterror="上传的文件过大,最大能上传2M";break;
case 2:$this->seterror="上传的文件过大,最大能上传2M";break;
case 3:$this->seterror="文件只有部分被上传";break;
case 4:$this->seterror="文件没有被上传";break;
case 6:$this->seterror="找不到临时文件夹";break;
case 7:$this->seterror="文件写入失败";break;
}
}
else
{
if(!in_array($this->doctype,$allowtype))
{
$this->seterror="文件类型不符";
}
else
{
if(!is_uploaded_file($this->doctmpname))
{
$this->seterror="非法操作";
}
else
{
if(!file_exists($this->path))
{
mkdir($this->path,07777);
}
else
{
if(!move_uploaded_file($this->doctmpname,$this->path.$this->docnewname))
{
$this->seterror="文件上传失败";
}
}
}
}
}
}
function get_img_newname()
{
if($this->seterror!=1)
{
echo $this->seterror;exit;
}
else
{
return $this->path.$this->docnewname;
}
}
}
?>