<?php
/**
*照片上传类
*
**/

//include "../../system/bmp.php";
//include_once '../admin/check_master.php';
//$MaxSize = $smtfilesize*1024*1024;
class go_upload{

	/**
* 表单中 文件框<input type="file" id="FileName">名称
* @var string
*/
	var $FileName = "";

	/**
* 上传目录
* @var string
*/
	var $Directroy = "";

	/**
* 最大文件大小
* @var int
*/
	
	var $MaxSize1 = 2097152;

	/**
* 是否可以上传
* @var bool
*/
	var $CanUpload = true;

	/**
* 上传文件名
* @var string
*/
	var $doUpFile = '';

	/**
* 缩略图名
* @var string
*/
	var $sm_File = '';

	/**
* 异常号
* @var int
*/
	var $Error = 0;

	/**
* 构造函数
*
* @param string $FileName
* @param string $dirPath
* @param int $maxSize
* @return null
*/
	function go_upload($FileName='', $dirPath='', $maxSize=2097152) //(1024*2)*1024=2097152 就是 2M
	{
		//global $FileName, $Directroy, $MaxSize, $CanUpload, $Error, $doUpFile, $sm_File;
		//初始化各种参数
		$this->FileName = $FileName;
		$this->MaxSize = $maxSize;
		if ($FileName == ''){
			$this->CanUpload = false;
			$this->Error = 1;
			return -1;
		}

		if ($dirPath != ''){
			$this->Directroy = $dirPath;
		}else{
			$this->Directroy = './';
		}
	}


	/**
* 检查文件是否存在
*
* @return bool
*/
	function scanFile()
	{
		if ($this->CanUpload){
			$scan = is_readable($_FILES[$this->FileName]['name']);
			if ($scan){
				$Error = 2;
			}
			return $scan;
		}
	}


	/**
* 获取文件大小
*
* @return int
*/
	function getSize($format = 'B')
	{

		if ($this->CanUpload){
			if ($_FILES[$this->FileName]['size'] == 0){
				$this->Error = 3;
				$this->CanUpload = false;
			}
			switch ($format){
				case 'B':
					return $_FILES[$this->FileName]['size'];
					break;

				case 'M':
					return ($_FILES[$this->FileName]['size'])/(1024*1024);
				case 'K':
					return ($_FILES[$this->FileName]['size'])/1024;
			}
		}
	}



	/**
* 获取文件类型
*
* @return string
*/
	function getExt()
	{
		if ($this->CanUpload){
			$ext=$_FILES[$this->FileName]['name'];
			$extStr=explode('.',$ext);
			$count=count($extStr)-1;
		}
		return $extStr[$count];
	}


	/**
* 获取文件名称
*
* @return string
*/
	function getName($i)
	{
		if ($this->CanUpload){
			return $_FILES[$this->FileName]['name'][$i];
		}

	}

	/**
* 新建文件名
*
* @return string
*/
	function newName(&$ext,$i)
	{
		if ($this->CanUpload){
			$FullName=$_FILES[$this->FileName]['name'][$i];
			$extStr=explode('.',$FullName);
			$count=count($extStr)-1;
			$ext = $extStr[$count];
			return date('YmdHis').rand(0,9).'.'.$ext;
		}
	}

	function muchpicNewName(&$ext)
	{
		if ($this->CanUpload){
			$FullName=$_FILES[$this->FileName]['name'];
			$extStr=explode('.',$FullName);
			$count=count($extStr)-1;
			$ext = $extStr[$count];
			return date('YmdHis').rand(0,9).'.'.$ext;
		}
	}



	/**
* 上传文件，失败时返回异常类型号
*
* @param   string   $fileName
* @return   
*/ 
	function upload($i,$fileName = '')
	{
		try{
			if ($this->CanUpload){
				if ($_FILES[$this->FileName]['size'] == 0){
					$this->Error = 3;
					$this->CanUpload = false;
					return $this->Error;
				}
			}

			if($this->CanUpload){
				if ($fileName == ''){
					$fileName = $_FILES[$this->FileName]['name'][$i];
				}
				//echo "name=".$_FILES[$this->FileName]['tmp_name'][$i]."<br />";
				//echo "Directroy=".$this->Directroy.$fileName."<br />";
				$this->doUpload=move_uploaded_file($_FILES[$this->FileName]['tmp_name'][$i], $this->Directroy.$fileName);

				if($this->doUpload)
				{
					$this->doUpFile = $fileName;
					chmod($this->Directroy.$fileName, 0777);
					return true;
				}else{
					$this->Error = 4;
					return $this->Error;
					//				throw new UploadException ('Driver not found:'.'SealUpload'.$type);
				}
			}
		}
		catch(SealUploadException $e)
		{
			$instance = SealLogMgr::getInstance();
			$instance->writeLog("[upload file failed] ".$e->getMessage(), '/upload/', __CLASS__, 'M');
		}
	}

	function uploadonly($fileName = '')
	{
		if ($this->CanUpload){
			if ($_FILES[$this->FileName]['size'] == 0){
				$this->Error = 3;
				$this->CanUpload = false;
				return $this->Error;
			}
		}
		$path = $this->Directroy.$fileName;
		if($this->CanUpload){
			if ($fileName == ''){
				$fileName = $_FILES[$this->FileName]['name'];
			}
			//echo "name=".$_FILES[$this->FileName]['tmp_name'][$i]."<br />";
			//echo "Directroy=".$this->Directroy.$fileName."<br />";
			$this->doUpload=move_uploaded_file($_FILES[$this->FileName]['tmp_name'], $this->Directroy.$fileName);
			if($this->doUpload)
			{
				$this->doUpFile = $fileName;
				chmod($this->Directroy.$fileName, 0777);
				return true;
			}else{
				$this->Error = 4;
				return $this->Error;
				//				throw new UploadException ('Driver not found:'.'SealUpload'.$type);
			}
		}
	}

	function muchpicUpload($orderNum,$fileName = '')
	{
		if ($this->CanUpload)
		{
			if ($_FILES[$this->FileName]['error'])
			{
				$this->Error ="uploadNext('$orderNum:1')";
				$this->CanUpload = false;
				return $this->Error;
			}
			if ($fileName == ''){
				$fileName = $_FILES[$this->FileName]['name'];
			}
			$this->doUpload=move_uploaded_file($_FILES[$this->FileName]['tmp_name'], $this->Directroy.$fileName);
			if($this->doUpload)
			{
				$this->doUpFile = $fileName;
				chmod($this->Directroy.$fileName, 0777);
				return "uploadNext('$orderNum:0')";
			}
			else
			{
				$this->Error ="uploadNext('$orderNum:1')";
				return $this->Error;

			}
		}
	}



	/**
* 网上上传文件，失败时返回异常类型号
*
* @param   string   $fileName
* @return   
*/ 
	function webupload($file_url,$fileName = ''){
		if($this->CanUpload){
			$this->doUpload=file_get_contents($file_url);
			$url_head=(get_headers($file_url));
			if ($url_head[0]=="HTTP/1.1 200 OK"){

				if($this->doUpload){
					//				echo "file_name=".$this->Directroy.$fileName."<hr>";
					$handle = fopen($this->Directroy.$fileName,"a+");
					fwrite($handle, $this->doUpload);
					fclose($handle);
					chmod($this->Directroy.$fileName, 0777);
					$this->doUpFile = $fileName;
					return true;
				}else{
					$this->Error = 4;
					return false;
				}
			}else{
				return false;
			}
		}
	}

	/**
* 创建图片缩略图,成功返回真，否则返回错误类型号
*
* @param string $dscChar   前缀
* @param int $width    缩略图宽
* @param int $height   缩略图高
* @return
*/
	function thumb($dscChar='',$type,$toW=160,$toH=120){

		if ($this->CanUpload && $this->doUpFile != ''){

			$srcFile = $this->doUpFile;
			if ($dscChar == ''){
				$dscChar = 'sm_';
			}

			$dscFile = $this->Directroy.$dscChar.$srcFile;
			$data = @getimagesize($this->Directroy.$srcFile,&$info);
			switch ($data[2]) {
				case 1:
					$im = @imagecreatefromgif($this->Directroy.$srcFile);
					break;

				case 2:
					$im = @imagecreatefromjpeg($this->Directroy.$srcFile);
					break;

				case 3:
					$im = @imagecreatefrompng($this->Directroy.$srcFile);
					break;

				case 6:
					$im= @imagecreatefromwbmp($this->Directroy.$srcFile);
					break;
			}
			$srcW=imagesx($im);
			$srcH=imagesy($im);
			//echo "img:".$srcFile;
			$toWH  = $toW/$toH;
			$srcWH = $srcW/$srcH;
			if($toWH <= $srcWH){
				$width   = $toW;
				$height  = $width*($srcH/$srcW);
			}else{
				$height = $toH;
				$width  = $height*($srcW/$srcH);
			}
			if($height < 1) $height = 1;
			if($width < 1) $width = 1;
			if($height > $srcH) $height = $srcH;
			if($width > $srcW)  $width  = $srcW;
			$height = ceil($height);
			$width  = ceil($width);

			$ni = imagecreatetruecolor($width, $height);
			imagecopyresampled($ni, $im ,0,0,0,0,$width,$height,$srcW,$srcH);

			$cr = imagejpeg($ni,$dscFile);
			chmod($dscFile, 0777);
			//imagedestroy($im);
			if ($cr){
				$this->sm_File = $dscFile;
				return true;
			}else{
				$this->Error = 5;
				return $this->Error;
			}
		}
	}





	/**
* 返回错误类型号，用做异常处理
*
* @return int
*/
	function Err(){
		return $this->Error;
	}

	/**
* 上传后的文件名
*
* @return unknown
*/
	function UpFile(){
		if ($this->doUpFile != ''){
			return $this->doUpFile;
		}else{
			$this->Error = 6;
		}
	}


	/**
* 上传文件的路径
*
* @return unknown
*/
	function filePath(){
		if ($this->doUpFile != ''){
			return $this->Directroy.$this->doUpFile;
		}else{
			$this->Error = 6;
		}
	}


	/**
* 缩略图文件名称
*
* @return unknown
*/
	function thumbMap(){
		if ($this->sm_File != ''){
			return $this->sm_File;
		}else{
			$this->Error = 6;
		}
	}

	/**
* 版本信息
*
* @return unknown
*/
	function go_version(){
		return 'Ver 0.1';
	}
}

?>
