Base.class.php 5.57 KB
<?php 
/**
 * 文件上传的基类
 * 
 * @name Util_Upload_Base
 * @package util/upload
 * @version 1.0 (2012-08-14 18:46:51)
 * @author fei.hong <hf@yoho.cn>
 * @since 1.0
 */
class Util_Upload_Base
{
	/**
	 * 文件对象
	 *
	 * @var string
	 */
	protected $file;
	
	/**
	 * 存储路径
	 *
	 * @var string
	 */
	protected $filePath;
	
	/**
	 * 文件对象名字
	 *
	 * @var string
	 */
	protected $fileNames;
	
	/**
	 * 限制格式
	 *
	 * @var string
	 */
	protected $format = '/^image\/(pjpeg|jpe?g|gif|png|bmp)$/';
	
	/**
	 * 上传限制大小
	 *
	 * @var integer
	 */
	protected $maxFileSize = 8000000;
	
	/**
	 * 错误仓库
	 *
	 * @var array
	 */
	protected $errorWare = array();
	
	/**
	 * 新文件名
	 *
	 * @var string
	 */
	protected $newFile = '';
	
	/**
	 * 前缀
	 *
	 * @var string
	 */
	protected $prefix = '';
	
	/**
	 * 后缀
	 *
	 * @var string
	 */
	protected $suffix = '';

	/**
	 * fastdf类
	 * 
	 * @var object
	 */
	protected $fastdfs;
	
	/**
	 * 是否启用fastdfs
	 * 
	 * @var boolean
	 */
	protected $useFastdfs = true;
	protected $fastdfsQueueKey = 'eadda725eb4e8963763cfcb7c95accd4';
	protected $fastdfsQueueUrl ='http://www.yoho.cn/fragment/fasfdfs/setfastdfsqueue';
	
	/**
	 * 初始化
	 *
	 * @param array $file (文件)
	 * @param string $filePath (文件路径)
	 * @param string $inputName (指定的文件名)
	 * @param bollean $useFastdfs (是否用fastfds存储)
	 */
	public function __construct($file, $filePath, $inputName = null, $useFastdfs = false) 
	{
		if ($inputName !== null && $inputName !== array() && is_array($inputName)) 
		{
			$this->fileNames = $inputName;
		}
		elseif ($file !== array() && is_array($file)) 
		{
			$file = $this->removeArrayEmpty($file);
			$this->fileNames = array_keys($file);
		}
		$this->file = $file;
		$this->filePath = $filePath;
		$this->useFastdfs = defined('UPLOAD_FASTDFS_STATE') ? UPLOAD_FASTDFS_STATE : $useFastdfs;
		if ($this->useFastdfs !== false)
		{
			$this->fastdfs = new Util_Fastdfs();
		}
	}
	
	/**
	 * 去除空的数组
	 *
	 * @param array $data
	 * @return array
	 */
	public function removeArrayEmpty(array $data) 
	{
		$result = array();
		foreach ($data as $key => $val) 
		{
			if (!empty($val['name'])) 
			{
				$result[$key] = $val;
			}
		}
		return $result;
	}
	
	/**
	 * 设置验证格式
	 *
	 * @param string $str
	 */
	public function setFormat($str) 
	{
		if ($str !== '' && is_string($str)) 
		{
			$this->format = $str;
		}
	}
	
	/**
	 * 设置前缀
	 *
	 * @param string $str
	 */
	public function setPrefix($str) 
	{
		if ($str !== '' && is_string($str)) 
		{
			$this->prefix = $str;
		}
	}
	
	/**
	 * 是否开启fastdfs
	 * 
	 * @param boolean $state
	 */
	public function setFastdfs($state)
	{
		$this->useFastdfs = $state;	
	}
	
	/**
	 * 设置后缀
	 *
	 * @param string $str
	 */
	public function setSuffix($str) 
	{
		if ($str !== '' && is_string($str)) 
		{
			$this->suffix = $str;
		}
	}
	
	/**
	 * 设置新的文件名
	 *
	 * @param string $fileName (文件名)
	 */
	public function setNewFile($newFile)
	{
		if ($newFile !== '' && is_string($newFile)) 
		{
			$this->newFile = $newFile;
		}
	}
	
	/**
	 * 设置上传大小
	 *
	 * @param integer $size (文件大小)
	 */
	public function setMaxFileSize($size) 
	{
		$size = (int) $size;
		if ($size > 0) 
		{
			$this->maxFileSize = $size;
		}
	}
	
	/**
	 * 错误提示 初始化
	 */
	public function initErrorWare() 
	{
		$this->errorWare = array('error' => array(), 'hit' => array(), );
	}
	
	/**
	 * 获取错误信息
	 *
	 * @param string $code (编码)
	 * @return string
	 */
	protected function errorMsg($code) 
	{
		$error = $this->error($code);
		return $error['mes'];
	}
	
	/**
	 * 设置fastfds队列
	 * 
	 * @param string $fileName
	 * @return boolean
	 */
	public function setFastdfsQueue($fileName)
	{
		$fileName = Util_Utils_AuthCode::encode($fileName, $this->fastdfsQueueKey);
		$data = array('fileName' => $fileName,'type' => 0);
		Util_Curl::post($this->fastdfsQueueUrl, $data);
	}
	
	/**
	 * 返回响应结果
	 *
	 * @param integer $code (编码)
	 * @param array $data (返回结果)
	 * @return array
	 */
	protected function error($code = 0, array $data = array()) 
	{
		$error = array();
		switch ($code) 
		{
			case 0:
				$error = array('code' => 0, 'mes' => '上传成功', 'result' => $data,);
				break;
			case 1:
				$error = array('code' => 1, 'mes' => '上传的文件超过了限制的值', 'result' => $data,);
				break;
			case 2:
				$error = array('code' => 2, 'mes' => '上传文件的大小超过了指定的值', 'result' => $data,);
				break;
			case 3:
				$error = array('code' => 3, 'mes' => '文件只有部分被上传', 'result' => $data,);
				break;
			case 4:
				$error = array('code' => 4, 'mes' => '没有文件被上传', 'result' => $data,);
				break;
			case 6:
				$error = array('code' => 6, 'mes' => '找不到临时文件夹', 'result' => $data,);
				break;
			case 7:
				$error = array('code' => 7, 'mes' => '文件写入失败', 'result' => $data,);
				break;
			case 8:
				$error = array('code' => 8, 'mes' => '非法上传', 'result' => $data,);
				break;
			case 9:
				$error = array('code' => 9, 'mes' => '上传失败', 'result' => $data,);
				break;
			case 10:
				$error = array('code' => 10, 'mes' => '不支持上传的文件类型', 'result' => $data,);
				break;
			case 11:
				$error = array('code' => 11, 'mes' => '移动文件失败', 'result' => $data,);
				break;
			case 12:
				$error = array('code' => 12, 'mes' => '上传信息', 'result' => $data,);
				break;
			default:
				$error = array('code' => -1, 'mes' => '综合错误', 'result' => $data,);
		}
		return $error;
	}
	
}