<?php
class Upload
{
private $doc; //文件
private $docsize; //文件大小
private $docname; //文件名字
private $doctype; //文件类型
private $docnewname; //图片新名字
private $allowtype; //可以上传的文件类型
private $seterror; //错误原因
private $doctmpname; //文件临时名
private $path; //文件存储路径
private $countover; //上传成功的图片个数
function __construct($file,$path)
{
$this->allowtype=Array("jpg","jpeg","gif","png","bmp");
$this->seterror=1;
$this->path=$path;
$this->docnewname=array();
$this->doctype=array();
$this->countover=0;
$this->doc=$file;
//判断是否存在错误
if(is_array($file['name']))
{
//判断是否有错误
foreach($file['error'] as $key => $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;
}
echo $this->seterror;exit;
}
}
//给文件起新名
foreach($file['name'] as $key =>$name)
{
$arr=explode(".",$name);
$this->doctype[$key]=$arr[count($arr)-1];
$this->docnewname[$key]=$this->path.time().$key.".".$this->doctype[$key];
}
//判断文件类型是否符合要求
foreach($this->doctype as $key => $type)
{
if(!in_array($type,$this->allowtype))
{
$this->seterror="第".++$key."张图片类型不符合要求";
echo $this->seterror;
exit;
}
}
//判断路径是否存在
if(!file_exists($path))
{
mkdir($path,07777);
}
//判断文件来源是否非法,否,上传图片
foreach($file['tmp_name'] as $key=>$tmpname)
{
if(!is_uploaded_file($tmpname))
{
$this->seterror="非法操作";
}
if($this->seterror==1)
{
$this->move_file();
}
}
}
}
private function move_file()
{
foreach($this->doc['tmp_name'] as $key=>$tmpname)
{
if(move_uploaded_file($tmpname,$this->docnewname[$key]))
{
$this->countover+=1;
}
else
{
echo "第".++$key."张图片上传失败";exit;
}
}
if($this->countover==count($this->doc['tmp_name']))
{
echo "全部上传成功";
}
}
}
?>