File.class.php 7.74 KB
<?php
/**
 * 文件上传
 * 
 * @name Util_Upload_File
 * @package util/upload
 * @version 1.1 (2013-06-14 17:24:52)
 * @author fei.hong <fei.hong@yoho.cn>
 * @since 1.0 (2012-08-14 18:46:51)
 */

class Util_Upload_File extends Util_Upload_Base implements Util_Upload_Interface
{
	/**
	 * 验证
	 *
	 * @return Array
	 */
	public function check() 
	{
		// 对file数据进行验证
		if (!is_string($this->filePath) || !is_array($this->file) || !isset($this->file[$this->fileNames[0]]))
		{
		    return $this->error(9, $this->file);
		}
		
		// 对所有不为空的文件进行验证
		$check = $this->checkSysError();
		if (array() !== $check['error'])
		{
			return $this->error($check['error'], $check);
		}
		
		// 对文件大小是否突破限制验证
		$check = $this->checkSize();
		if (array() !== $check['error']) 
		{
			return $this->error(2, $check);
		}
		
		//验证文件格式
 		$check = $this->checkFormat();
 		if (array() !== $check['error']) 
 		{
 			return $this->error(10, $check);
 		}
 		
		// 对是否是post过来的文件进行验证
		if (!is_uploaded_file($this->file[$this->fileNames[0]]['tmp_name'])) 
		{
			return $this->error(8, $this->file);
		}
		
		// 获取文件的唯一MD5值
		$data = array(); $md5 = null;
		foreach ($this->file as $id => $file)
		{
		    if (isset($file['tmp_name']))
		    {
		        $md5 = md5_file($file['tmp_name']);
		        if ($md5 !== false)
		        {
    		        $data[$id] = array('md5' => $md5,);
		        }
		    }
		}
		return $this->error(0, $data);
	}
	
	/**
	 * 写入文件
	 *
	 * @return array
	 */
	public function write() 
	{
		$this->initErrorWare();
		$ret = array();
		if ($this->useQiniu)
		{
			$ret = call_user_func(array($this->qiniuUploadClass, 'fileBatchUpload'), 
					array('file'=> $this->file, 'prefix' => $this->prefix,'filePath'=> $this->filePath));
		}
		else
		{
			$makePath = array(); $tmp = null;
			foreach ($this->file as $id => $file)
			{
				$makePath = $this->makePath($file);
				if ($this->createFolder($makePath['allpath']))
				{
					$tmp = $file['tmp_name']; unset($file['tmp_name']);
					if (!@move_uploaded_file($tmp, $makePath['file']))
					{
						$this->errorWare['error'][$id] = $file;
					}
					else
					{
						chmod($makePath['file'], 0755);
						$file['relaPath'] = $makePath['path'];
						$ret[$id] = $file;
					}
				}
				else //写入失败
				{
					return $this->error(7, $this->errorWare);
				}
			}
		}
		if(!empty($ret))
		{
			$this->errorWare['hit'] = $ret;
			return $this->error(12, $this->errorWare);
		}
		else
		{
			return $this->error(7, $this->errorWare);
		}
		
	}
	
	/**
	 * 写入文件
	 *
	 * @return array
	 */
	public function fastdfsWrite()
	{
		$this->initErrorWare();
		
		$makePath = array(); $tmp = null;
		foreach ($this->file as $id => $file)
		{
			$makePath = $this->makePath($file);
			$tmp = $file['tmp_name']; unset($file['tmp_name']);
			if (!$this->fastdfs->upload($makePath['file'], $tmp))
			{
				$this->errorWare['error'][$id] = $file;
			}
			else
			{
				$file['relaPath'] = $makePath['path'];
				$this->errorWare['hit'][$id] = $file;
				$this->setFastdfsQueue($makePath['file']);
			}
		}	
		
		return $this->error(12, $this->errorWare);
	}
	
	/**
	 * 写入单个文件
	 * 
	 * @param integer $id (文件ID)
	 * @return void
	 * @since 1.1
	 */
	public function writeOne($id)
	{	    
		$this->initErrorWare(); 
	    if (!isset($this->file[$id]))
	    {
	        return $this->error(4, $this->errorWare);
	    }
	    $ret = array();
        $file = $this->file[$id];
        if ($this->useQiniu)
        {
        	$ret = call_user_func(array($this->qiniuUploadClass, 'fileUpload'), array('file'=> $file, 
        			'prefix' => $this->prefix,'filePath'=> $this->filePath));
        }
        else 
        {
        	$makePath = $this->makePath($file);
        	if ($this->createFolder($makePath['allpath']))
        	{
        		$tmp = $file['tmp_name']; unset($file['tmp_name']);
        		if (@move_uploaded_file($tmp, $makePath['file']))
        		{
        			$file['relaPath'] = $makePath['path'];
        			@chmod($makePath['file'], 0755);
        		}
        		$ret = $file;
        	}
        }
        if(!empty($ret))
        {
        	$this->errorWare['hit'][$id] = $ret;
        	return $this->error(12, $this->errorWare);
        }
        else
        {
        	$this->errorWare['hit'][$id] = $file;
        	return $this->error(7, $this->errorWare);
        }
	}
	
	/**
	 * fastfs写入单个文件
	 * 
	 * @param integer $id (文件ID)
	 * @return void
	 * @since 1.1
	 */
	public function fastdfsWriteOne($id)
	{
		$this->initErrorWare();
		
		if (!isset($this->file[$id]))
		{
			return $this->error(4, $this->errorWare);
		}
		$file = $this->file[$id];
		$makePath = $this->makePath($file);
		$tmp = $file['tmp_name']; unset($file['tmp_name']);
		if ($this->fastdfs->upload($makePath['file'], $tmp))
		{
			$file['relaPath'] = $makePath['path'];
			$this->errorWare['hit'][$id] = $file;
			$this->setFastdfsQueue($makePath['file']);
		}
		else
		{
			$this->errorWare['error'][$id] = $file;
		}
		return $this->error(12, $this->errorWare);
	}
	
	/**
	 * 移除指定的文件
	 * 
	 * @ignore since 1.1 已不再使用
	 * @param integer $id (文件ID)
	 * @return void
	 */
	public function remove($id)
	{
	    if (isset($this->file[$id]))
	    {
	        unset($this->file[$id]);
	    }
	}
	
    /**
     * 创建文件夹
     * 
     * @param string $path
     * @return boolean
     */
    private function createFolder($path)
    {
        if (!is_dir($path)) 
        {
        	if(!$this->createFolder(dirname($path))) 
        	{
        		return false;
        	}
        	if (!mkdir($path, 0755)) 
        	{
        	    return false;
        	}
        }
        return true;
    }
	
	/**
	 * 组合新路径
	 *
	 * @param array $val
	 * @return array
	 */
	private function makePath($val) 
	{
	    $result = array();
	    if (is_array($val))
	    {
    		$fileInfo = pathinfo($val['name']);
    		if (empty($this->newFile)) // 默认
    		{
    			$datePath = date('/Y/m/d/H/');
    			$fileName = md5(date('H') . $val['name'] . $val['type'] . $val['size'] ) . '.' . strtolower($fileInfo['extension']);
    			$allPath = $this->filePath . $datePath;
    			$result['file'] = $allPath . $this->prefix . $fileName;
    			$result['allpath'] = $allPath;
    			$result['path'] = $datePath . $this->prefix . $fileName;
    		} 
    		else // 已指定生成的文件名
    		{
    			$allFilePath = $this->filePath . strtolower($this->prefix . $this->newFile);
    			$newFileInfo = pathinfo($allFilePath);
    			$result['file'] = $allFilePath;
    			$result['allpath'] = $newFileInfo['dirname'];
    			$result['path'] = strtolower($this->prefix . $this->newFile);
    		}
	    }
		return ($result);
	}
	
	/**
	 * 验证文件大小
	 *
	 * @return array
	 */
	private function checkSize() 
	{
		$this->initErrorWare();
		foreach ($this->file as $id => $file) 
		{
			if (intval($file['size']) > intval($this->maxFileSize) || intval($file['size']) <=0 ) 
			{
				$file['error_mes'] = $this->errorMsg($file['error']);
				$this->errorWare['error'][$id] = $file;
			}
		}
		return $this->errorWare;
	}
	
	/**
	 * 验证系统错误
	 *
	 * @return array
	 */
	private function checkSysError() 
	{
		$this->initErrorWare();
		foreach ($this->file as $id => $file) 
		{
			if (in_array($file['error'], array(1, 2, 3, 4, 6, 7,))) 
			{
				$file['error_mes'] = $this->errorMsg($file['error']);
				$this->errorWare['error'][$id] = $file;
			}
		}
		return $this->errorWare;
	}
	
	/**
	 * 验证文件格式
	 *
	 * @return array
	 */
	private function checkFormat() 
	{
		$this->initErrorWare();
		foreach ($this->file as $id => $file) 
		{
			if (!preg_match($this->format, $file['type'])) 
			{
				$this->errorWare['error'][$id] = $file;
			}
		}
		return $this->errorWare;
	}
	
}