<?php
/********************************************************************
 *                              DirCMS文件上传类
 *                            -------------------
 *   begin                : 2010-4-15
 *   copyright            : (C) 2009 DirCMS
 *   email                : uuturn@126.com
 *
 *   $Id: upload.class.php,2010 Sp1 2010-12-9 M Exp $
 ********************************************************************/
class upload
{
	public $savepath;
	public $files;
	private $error;

	function __construct($inputname, $savepath = '', $savename = '', $alowexts = array(),$maxsize = 1024000)
	{
		$maxsize=UPLOAD_SIZE;
		if(!$alowexts)$alowexts=explode('|',UPLOAD_FTYPE);
		$file_array=array();
		$savepath=str_replace('\\','/',$savepath);
		$savename=preg_replace('/[^a-z0-9_]+/i','',$savename);
		$this->savepath=substr($savepath,-1)=='/'?$savepath:$savepath.'/'; //路径名以/结尾

		if(!make_dir($this->savepath))
		{
			$this->error=8;
			$this->error();
		}
		//exit($this->savepath);
		if(!is_writeable($this->savepath))
		{
			$this->error=9;
			$this->error();
		}
		if(sizeof($_FILES[$inputname]['error'])>25)
		{
			$this->error=13;
			$this->error();
		}
		$max=sizeof($_FILES[$inputname]['error'])-1;
		//exit($this->savepath.$savename);
		foreach($_FILES[$inputname]['error'] as $key => $error)
		{
			if($error==UPLOAD_ERR_OK) //批量上传
			{
				$savename=$savename?$savename:date('Ymdims').mt_rand(10000,99999); 
				$fileext=strtolower(get_fileext($_FILES[$inputname]['name'][$key]));
				$savename=$savename.'.'.$fileext;
				$tmp_name=$_FILES[$inputname]['tmp_name'][$key];
				$filesize=$_FILES[$inputname]['size'][$key];

				if(!in_array($fileext,$alowexts))
				{
					if($file_array)foreach($file_array as $v)
					{
						@unlink(DIRCMS_ROOT.$v);
					}
					$this->error=10;
					$this->error();
				}
				if($filesize>$maxsize)
				{
					if($file_array)foreach($file_array as $v)
					{
						@unlink(DIRCMS_ROOT.$v);
					}
					$this->error=11;
					$this->error();
				}
				if(!$this->isuploadedfile($tmp_name))
				{
					if($file_array)foreach($file_array as $v)
					{
						@unlink(DIRCMS_ROOT.$v);
					}
					$this->error=12;
					$this->error();
				}

				if(move_uploaded_file($tmp_name,$this->savepath.$savename) || @copy($tmp_name,$this->savepath.$savename))
				{
					@chmod($savename, 0777);
					@unlink($tmp_name);
					$file_array[]=$this->savepath.$savename;					
				}
			}
			else
			{
				if($file_array)foreach($file_array as $v)
				{
					@unlink(DIRCMS_ROOT.$v);
				}
				$this->error=$error;
				if($error==4)
				{
					return false;
				}
				$this->error();
			}
			unset($savename);
		}
		$this->files=$file_array;
		return true;
	}

	function isuploadedfile($file) //去掉系统自带的反斜线
	{
		return (is_uploaded_file($file) || is_uploaded_file(str_replace('\\\\','\\',$file))); 
	}

	function error()
	{
		$upload_error=array(0 => '文件上传成功 !',
							1 => '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值 !',
							2 => '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值 !',
							3 => '文件只有部分被上传 !',
							4 => '没有文件被上传 !',
							5 => '未知错误!',
							6 => '找不到临时文件夹。 !',
							7 => '文件写入临时文件夹失败 !',
							8 => '附件目录创建失败 !',
							9 => '附件目录没有写入权限 !',
							10 => '不允许上传的文件类型 !',
							11 => '文件超过了管理员限定的大小 !',
							12 => '非法上传文件 !',
							13 => '最多可同时上传25个文件 !'
							);
		showmsg($upload_error[$this->error]);
	}

}
?>