FileStream.class.php 3.75 KB
<?php
/**
 * 文件流上传
 * 
 * @name Util_Upload_FileStream
 * @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_FileStream extends Util_Upload_Base implements Util_Upload_Interface
{
	/**
	 * 验证
	 *
	 * @return Array
	 */
	public function check() 
	{
		// 对file数据进行验证
		if (!is_string($this->filePath) || empty($this->file)) 
		{
			return $this->error(9, $this->file);
		}

		// 对文件大小是否突破限制验证
		$check = $this->checkSize();
		if (array() !== $check['error']) 
		{
			return $this->error(2, $check);
		}
		
		return $this->error(0, array(0 => array('md5' => md5($this->file)),));
	}
	
	/**
	 * 写入文件
	 *
	 * @return array
	 */
	public function write() 
	{
		if ($this->useFastdfs)
		{
			return $this->fastdfsWrite();
		}
		
		$this->initErrorWare();
		if (is_string($this->file))
		{
    		$makePath = $this->makePath();
    		if (false === $this->createFolder($makePath['allpath']))
    		{
    		    return $this->error(7, $this->errorWare);
    		}
    		file_put_contents($makePath['file'], $this->file);
    		$this->errorWare['hit'][0]['relaPath'] = $makePath['path'];
    		@chmod($makePath['file'], 0755);
		}
		return $this->error(12, $this->errorWare);
	}
	
	/**
	 * fastdfs写入文件
	 *
	 * @return array
	 */
	public function fastdfsWrite()
	{
		$this->initErrorWare();
		if (is_string($this->file))
		{
			$makePath = $this->makePath();
			if (!$this->fastdfs->uploadByStream($makePath['file'], $this->file))
			{
				$this->errorWare['error'][$id] = 'error stream';
			}
			else
			{
				$this->errorWare['hit'][0]['relaPath'] = $makePath['path'];
				$this->setFastdfsQueue($makePath['file']);
			}
		}
		return $this->error(12, $this->errorWare);
	}
	
	/**
	 * 写入单个文件
	 * 
	 * @param integer $id (文件ID)
	 * @return array
	 * @since 1.1
	 */
	public function writeOne($id)
	{
		if ($this->useFastdfs)
		{
			return $this->fastdfsWrite();
		}
		else
		{
			return $this->write();
		}
	}
	
	/**
	 * 移除指定的文件
	 *
	 * @ignore since 1.1 已不再使用
	 * @param integer $id (文件ID)
	 * @return void
	 */
	public function remove($id)
	{
		if (isset($this->file[$id]))
		{
			unset($this->file[$id]);
		}
		else
		{
		    unset($this->file);
		}
	}
	
	/**
	 * 组合新路径
	 *
	 * @return array
	 */
	private function makePath() 
	{
		if (empty($this->newFile)) // 默认
		{
			$datePath = date('/Y/m/d/H/');
			$fileName = md5(microtime()) . '.jpg'; // 文件名以时间md5值命名
			$allPath = $this->filePath . $datePath;
			$allFilePath = $allPath . $this->prefix . $fileName;
			$newPath = $datePath . $this->prefix . $fileName;
		} 
		else // 已指定生成的文件名
		{
			$allFilePath = $this->filePath . strtolower($this->prefix . $this->newFile);
			$newFileInfo = pathinfo($allFilePath);
			$allPath = $newFileInfo['dirname'] . '/';
			$newPath = strtolower($this->prefix . $this->newFile);
		}
		return array('file' => $allFilePath, 'allpath' => $allPath, 'path' => $newPath );
	}
	
    /**
     * 创建文件夹
     * 
     * @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;
    }
    
    /**
	 * 验证文件大小
	 *
	 * @return array
	 */
	private function checkSize() 
	{
		$this->initErrorWare();
		if (strlen($this->file) > $this->maxFileSize || strlen($this->file) <=0 ) 
		{
		    $val['error_mes'] = $this->errorMsg(2);
			$this->errorWare['error'][0] = $val;
		}
		return $this->errorWare;
	}
	
}