<?php
/*
文件上传类

*/

class upload
{
	//大范围的可传文件
	private $allowfile=array("gif",'png','bmp','jpg','jpeg','swf','rar','zip','7z','doc','txt','xsl','mp3','mp4','rm','mp2','rmvb','3gp');
	//用户自定义的可传文件类型
	public $uallowfile=array();
	public $maxsize=10240000;
	public $filedir="attach";
	function __construct($uallowfile=array())
	{
		$this->uallowfile=$uallowfile;
		if(empty($this->uallowfile))
		{
			$this->uallowfile=$this->allowfile;
		}
	}
	
	function uploadfile($file)
	{
		
		$files=$_FILES[$file];
		if(is_array($files['name']))
		{
			$fname=array();
			foreach($files['name'] as $key=> $val)
			{
				$err=0;
				if($files['error'][$key])
				{
					$err=1;	
				}
				
				if($files['size'][$key]>$this->maxsize)
				{
					$err=1;
				}
				
				if(!in_array($this->getftype($files['name'][$key]),$this->allowfile) || !in_array($this->getftype($files['name'][$key]),$this->uallowfile))
				{					
					$err=1;					
				}
				if($err)
				{
					@unlink($files['tmp_name'][$key]);
				}else
				{
					$dir=$this->filedir."/".date("Ym");
					$this->umkdir($dir);
					$tofile=$dir."/".date("YmdHis").rand(1,1000).".".$this->getftype($files['name'][$key]);
					$i=1;
					$unfile=0;
					while($unfile==0)
					{
						if(file_exists($tofile))
						{
							$tofile= $dir."/".date("YmdHis").rand(1,1000)."_{$i}.".$this->getftype($files['name'][$key]);
						}else
						{
							$unfile=1;
						}
						$i++;
					}
					if(!$this->move_file($files['tmp_name'][$key],$tofile))
					{
						return array("error","文件目录不可写！");
					}else
					{
						$fname[]=$tofile;
					}
				}

			}
			return array("filename"=>$fname);//多文件上传结束
		}else
		{
			$file=$files;
		
			if($file['error'])
			{
				return array("error"=>'文件上传出错');
				@unlink($file['tmp_name']);
				exit();			
			}
			
			if($file['size']>$this->maxsize)
			{
				return array("error"=> '文件过大');
				@unlink($file['tmp_name']);
				exit();	
			}
			
			if(!in_array($this->getftype($file['name']),$this->allowfile) || !in_array($this->getftype($file['name']),$this->uallowfile))
			{
				return array("error"=>"文件类型不允许");
				@unlink($file['tmp_name']);
				exit();	
			}
			$dir=$this->filedir."/".date("Ym");
			$this->umkdir($dir);
			$tofile=$dir."/".date("YmdHis").".".$this->getftype($file['name']);
			$i=1;
			$unfile=0;
			while($unfile==0)
			{
				if(file_exists($tofile))
				{
					$tofile= $dir."/".date("YmdHis")."_{$i}.".$this->getftype($files['name'][$key]);
				}else
				{
					$unfile=1;
				}
				$i++;
			}
			if($this->move_file($file['tmp_name'],$tofile))
			{
				return array("filename"=>$tofile,"filetype"=>$this->getftype($file['name']));
			}else
			{
				return array("error","文件目录不可写！");
			}
		}
	}
	
	function move_file($from,$tofile)
	{	
		if(move_uploaded_file($from,$tofile))
		{
			return true;
		}else
		{
			@unlink($from);
			return false;
		}
	}
	//获取文件的后缀
	function getftype($file)
	{
		return strtolower(substr(strrchr($file,"."),1));
	}
	
	function umkdir($dir)
	{
		$arr=explode("/",$dir);
		foreach($arr as $key=>$val)
		{
			$d="";
			for($i=0;$i<=$key;$i++)
			{
				$d.=$arr[$i]."/";
			}
			if(!file_exists($d) && (strpos($val,":")==false))
			{
				mkdir($d,0755);
			}
		}
	}
	
}

?>