2014-12-26 13:45:05 +00:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: liujin834
|
|
|
|
|
* Date: 14/12/26
|
|
|
|
|
* Time: 下午3:52
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Westdc\File;
|
|
|
|
|
|
|
|
|
|
use Zend\ServiceManager\ServiceManager;
|
|
|
|
|
use Zend\ServiceManager\ServiceManagerAwareInterface;
|
|
|
|
|
use Westdc\EventModel\AbstractEventManager;
|
2014-12-27 16:03:41 +00:00
|
|
|
|
use Westdc\File\Listener\DefaultFileUploadListener;
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
|
|
|
|
class Upload extends AbstractEventManager implements ServiceManagerAwareInterface{
|
|
|
|
|
|
|
|
|
|
protected $serviceManager;
|
2015-01-13 09:24:11 +00:00
|
|
|
|
protected $defaultListener = false;
|
2015-01-14 08:49:10 +00:00
|
|
|
|
protected $returnInPreCheckTrigger = true;
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
|
|
|
|
private $uploadPath = "";
|
|
|
|
|
private $relativePath = "";
|
|
|
|
|
private $fileName = "";
|
2015-03-27 07:50:44 +00:00
|
|
|
|
private $fileUuid;
|
2014-12-26 13:45:05 +00:00
|
|
|
|
private $config;
|
2014-12-28 15:37:16 +00:00
|
|
|
|
private $params;
|
2015-03-25 02:35:21 +00:00
|
|
|
|
private $rootPath,$childPath,$datePathMode;
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
2014-12-27 16:33:12 +00:00
|
|
|
|
//日期路径模式
|
2014-12-27 16:03:41 +00:00
|
|
|
|
const DATETIME_MODEL_YMD = "Y/M/D/";
|
|
|
|
|
const DATETIME_MODEL_YM = "Y/M/";
|
|
|
|
|
const DATETIME_MODEL_Y = "Y/";
|
|
|
|
|
|
2014-12-26 13:45:05 +00:00
|
|
|
|
public function setServiceManager(ServiceManager $serviceManager)
|
|
|
|
|
{
|
|
|
|
|
$this->serviceManager = $serviceManager;
|
|
|
|
|
|
|
|
|
|
$this->init();
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function init()
|
|
|
|
|
{
|
|
|
|
|
$configService = $this->serviceManager->get('ConfigService');
|
|
|
|
|
$this->config = $configService->get('file.php');
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 02:35:21 +00:00
|
|
|
|
public function __invoke($files,$rootDir = "",$childDir = "",$fileName = "",$dateDirModel = false)
|
2014-12-27 16:33:12 +00:00
|
|
|
|
{
|
2014-12-28 15:37:16 +00:00
|
|
|
|
return $this->upload($files,$rootDir,$childDir,$fileName,$dateDirModel);
|
2014-12-27 16:33:12 +00:00
|
|
|
|
}
|
2014-12-27 16:03:41 +00:00
|
|
|
|
|
2015-01-12 12:59:23 +00:00
|
|
|
|
/**
|
|
|
|
|
* 添加默认侦听器,会将信息保存到Attachments数据表
|
|
|
|
|
*/
|
|
|
|
|
public function attachDefaultListener()
|
|
|
|
|
{
|
|
|
|
|
$Listener = new DefaultFileUploadListener;
|
|
|
|
|
$this->getEventManager()->attachAggregate($Listener);
|
2015-01-13 09:24:11 +00:00
|
|
|
|
$this->defaultListener = true;
|
2015-01-12 12:59:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-27 16:33:12 +00:00
|
|
|
|
/**
|
|
|
|
|
* 上传文件
|
|
|
|
|
* @param $files 上传文件的信息 e.g.$_FILE['fileData']
|
|
|
|
|
* @param string $rootDir 文件存储的根路径(磁盘物理路径),如果没有设置则从 config/autoload/file.php 中读取 upload 配置
|
|
|
|
|
* @param string $childDir 根路径中的下一级路径,默认为空
|
|
|
|
|
* @param string $fileName 新文件的文件名,如果不指定则设置为新的UUID
|
2015-03-25 02:35:21 +00:00
|
|
|
|
* @param string|bool $dateDirModel 年月日目录模式,如果设为 false 则存储时不添加年月日路径,年月日路径在子文件夹中创建,unix和linux中需要开启上传目录的权限,否则无法创建文件夹
|
2014-12-27 16:33:12 +00:00
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2015-03-25 02:35:21 +00:00
|
|
|
|
public function upload($files,$rootDir = "",$childDir = "",$fileName = "",$dateDirModel = "")
|
2014-12-26 13:45:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (empty($files) !== false) {
|
|
|
|
|
return array("error"=>"请选择要上传的文件.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_uploaded_file($files['tmp_name']) === false) {
|
|
|
|
|
return array("error"=>"文件上传失败,请重新上传");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$file = $files;
|
|
|
|
|
|
|
|
|
|
$results = $this->getEventManager()->trigger('upload.pre', $this, compact('file'));
|
|
|
|
|
|
2015-01-14 08:49:10 +00:00
|
|
|
|
if($this->returnInPreCheckTrigger === true)
|
2014-12-26 13:45:05 +00:00
|
|
|
|
{
|
2015-01-13 09:24:11 +00:00
|
|
|
|
$cache_data = $results->last();
|
|
|
|
|
|
|
|
|
|
if($cache_data !== true)
|
|
|
|
|
{
|
|
|
|
|
return $cache_data;
|
|
|
|
|
}
|
2014-12-26 13:45:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-27 16:03:41 +00:00
|
|
|
|
$fileService = $this->serviceManager->get('File');
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
2015-03-25 02:35:21 +00:00
|
|
|
|
if(!empty($rootDir))
|
|
|
|
|
$this->setRootDir($rootDir);
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
2015-03-25 02:35:21 +00:00
|
|
|
|
$this->MakeRootDir();
|
|
|
|
|
|
|
|
|
|
if(!empty($childDir))
|
|
|
|
|
$this->setChildDir($childDir);
|
|
|
|
|
|
2015-04-07 02:13:14 +00:00
|
|
|
|
//$this->MakeChildDir($childDir);
|
2015-03-25 02:35:21 +00:00
|
|
|
|
|
|
|
|
|
if(!empty($dateDirModel))
|
|
|
|
|
$this->setDatePathMode($dateDirModel);
|
|
|
|
|
|
|
|
|
|
$this->makeDateDir();
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
2015-04-30 10:47:18 +00:00
|
|
|
|
if(empty($this->fileName) && empty($fileName))
|
2015-01-13 09:24:11 +00:00
|
|
|
|
$this->setFileName(NULL , $fileService->getFileTextExt($files['name']));
|
|
|
|
|
|
|
|
|
|
if(!empty($fileName))
|
|
|
|
|
$this->setFileName($fileName , $fileService->getFileTextExt($files['name']));
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
|
|
|
|
//移动文件
|
2014-12-27 16:03:41 +00:00
|
|
|
|
$file_path = $this->getUploadPath() . $this->getFileName();
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
|
|
|
|
if (move_uploaded_file($file['tmp_name'], $file_path) === false) {
|
|
|
|
|
return array("error"=>"上传失败,请重试");
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-27 16:03:41 +00:00
|
|
|
|
$file_data = array();
|
2014-12-28 15:37:16 +00:00
|
|
|
|
|
2014-12-27 16:03:41 +00:00
|
|
|
|
$file_data['file_url'] = $this->getRelativePath() . $this->getFileName();
|
|
|
|
|
$file_data['file_size'] = $files['size'];
|
|
|
|
|
$file_data['db_path'] = $this->getRelativePath() . $this->getFileName();
|
|
|
|
|
$file_data['realname'] = $files['name'];
|
|
|
|
|
$file_data['file_ext'] = $fileService->getFileTextExt($files['name']);
|
2014-12-28 15:37:16 +00:00
|
|
|
|
$file_data['file_mime'] = $fileService->getFileMime($file_path);
|
2015-03-27 07:50:44 +00:00
|
|
|
|
$file_data['uuid'] = $this->fileUuid;
|
2014-12-28 15:37:16 +00:00
|
|
|
|
|
2015-01-13 09:24:11 +00:00
|
|
|
|
if(!empty($this->params) && is_array($this->params))
|
2014-12-28 15:37:16 +00:00
|
|
|
|
{
|
|
|
|
|
$file_data = array_merge($file_data,$this->params);
|
|
|
|
|
}
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
2014-12-27 16:03:41 +00:00
|
|
|
|
$results = $this->getEventManager()->trigger('upload.after', $this, compact('file_data'));
|
|
|
|
|
$cache_data = $results->last();
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
2014-12-27 16:03:41 +00:00
|
|
|
|
if(is_array($cache_data))
|
|
|
|
|
$file_data = array_merge($file_data , $cache_data);
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
2014-12-27 16:03:41 +00:00
|
|
|
|
return $file_data;
|
2014-12-26 13:45:05 +00:00
|
|
|
|
}//文件上传
|
|
|
|
|
|
|
|
|
|
/**
|
2015-03-25 02:35:21 +00:00
|
|
|
|
* 设置上传根目录
|
|
|
|
|
* @param string $path
|
|
|
|
|
*/
|
|
|
|
|
public function setRootDir($path = ""){
|
|
|
|
|
$this->rootPath = $path;
|
2015-04-08 03:22:15 +00:00
|
|
|
|
|
|
|
|
|
if(empty($this->rootPath))
|
|
|
|
|
$this->uploadPath = $this->config->upload;
|
|
|
|
|
else
|
|
|
|
|
$this->uploadPath = $this->rootPath;
|
|
|
|
|
|
|
|
|
|
if(!preg_match("/[\/|\\\]+$/",$this->uploadPath))
|
|
|
|
|
$this->uploadPath .= "/";
|
2015-03-25 02:35:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取上传文件的根路径
|
2014-12-26 13:45:05 +00:00
|
|
|
|
* 根路径需要自行创建
|
|
|
|
|
* 路径结尾必须加 "/"
|
2014-12-27 16:03:41 +00:00
|
|
|
|
* @return bool
|
2014-12-26 13:45:05 +00:00
|
|
|
|
*/
|
2015-03-25 02:35:21 +00:00
|
|
|
|
public function MakeRootDir()
|
2014-12-26 13:45:05 +00:00
|
|
|
|
{
|
2015-04-08 03:22:15 +00:00
|
|
|
|
if(!file_exists($this->rootPath)){
|
|
|
|
|
mkdir($this->rootPath);
|
|
|
|
|
}
|
2015-03-25 02:35:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setChildDir($dir = ""){
|
2015-04-08 03:22:15 +00:00
|
|
|
|
|
2015-04-08 03:50:47 +00:00
|
|
|
|
if(empty($dir)) {
|
2015-04-08 03:22:15 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 03:50:47 +00:00
|
|
|
|
if (!preg_match("/[\/|\\\]+$/", $dir))
|
|
|
|
|
$dir .= "/";
|
2015-04-08 03:22:15 +00:00
|
|
|
|
|
2015-04-08 03:50:47 +00:00
|
|
|
|
$this->uploadPath .= $dir;
|
2015-04-08 03:22:15 +00:00
|
|
|
|
if(!file_exists($this->uploadPath)) {
|
|
|
|
|
if(!mkdir($this->uploadPath))
|
|
|
|
|
return "failed to create folder :".$this->uploadPath;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 03:50:47 +00:00
|
|
|
|
$this->childPath = $dir;
|
2015-04-08 03:22:15 +00:00
|
|
|
|
$this->relativePath .= $this->childPath;
|
2015-04-07 02:13:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function appendChildDir($dir = ""){
|
2015-04-08 03:22:15 +00:00
|
|
|
|
|
|
|
|
|
if(empty($dir)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!preg_match("/[\/|\\\]+$/", $dir))
|
|
|
|
|
$dir .= "/";
|
|
|
|
|
|
|
|
|
|
$this->uploadPath .= $dir;
|
2015-04-08 03:23:12 +00:00
|
|
|
|
|
2015-04-08 03:22:15 +00:00
|
|
|
|
if(!file_exists($this->uploadPath)) {
|
|
|
|
|
if(!mkdir($this->uploadPath))
|
|
|
|
|
return "failed to create folder :".$this->uploadPath;
|
|
|
|
|
}
|
2015-04-07 02:13:14 +00:00
|
|
|
|
$this->childPath .= $dir;
|
2015-04-08 03:50:47 +00:00
|
|
|
|
$this->relativePath .= $dir;
|
2015-04-08 03:22:15 +00:00
|
|
|
|
|
2014-12-26 13:45:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置子路径,自动加在根路径之后
|
|
|
|
|
* 如果不存在程序将创建
|
2014-12-27 16:03:41 +00:00
|
|
|
|
* @return bool | string
|
2014-12-26 13:45:05 +00:00
|
|
|
|
*/
|
2015-04-08 03:22:15 +00:00
|
|
|
|
public function MakeChildDir($dir)
|
2014-12-26 13:45:05 +00:00
|
|
|
|
{
|
2015-04-08 03:22:15 +00:00
|
|
|
|
if(empty($dir)) {
|
2014-12-27 16:03:41 +00:00
|
|
|
|
return true;
|
2014-12-26 13:45:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 03:22:15 +00:00
|
|
|
|
if (!preg_match("/[\/|\\\]+$/", $dir))
|
|
|
|
|
$dir .= "/";
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
2015-04-08 03:22:15 +00:00
|
|
|
|
$this->uploadPath .= $dir;
|
2014-12-27 16:03:41 +00:00
|
|
|
|
if(!file_exists($this->uploadPath)) {
|
|
|
|
|
if(!mkdir($this->uploadPath))
|
|
|
|
|
return "failed to create folder :".$this->uploadPath;
|
|
|
|
|
}
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
2015-04-08 03:22:15 +00:00
|
|
|
|
$this->relativePath = $dir;
|
2014-12-26 13:45:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建并返回年月日的子目录路径
|
2015-03-25 02:35:21 +00:00
|
|
|
|
* @param string $model
|
|
|
|
|
* @return string|bool
|
2014-12-26 13:45:05 +00:00
|
|
|
|
*/
|
2015-03-25 02:35:21 +00:00
|
|
|
|
public function makeDateDir($model = "")
|
2014-12-26 13:45:05 +00:00
|
|
|
|
{
|
2015-03-25 02:35:21 +00:00
|
|
|
|
if($model == ''){
|
|
|
|
|
$model = $this->getDatePathMode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$current_path = "";
|
|
|
|
|
|
|
|
|
|
if(empty($model) || $model == false)
|
|
|
|
|
return $current_path;
|
|
|
|
|
|
2014-12-26 13:45:05 +00:00
|
|
|
|
$y = date("Y");
|
|
|
|
|
$m = date("m");
|
|
|
|
|
$d = date("d");
|
|
|
|
|
|
2014-12-27 16:03:41 +00:00
|
|
|
|
if($model == self::DATETIME_MODEL_YMD || $model == self::DATETIME_MODEL_YM || $model == self::DATETIME_MODEL_Y) {
|
|
|
|
|
$current_path = $y . "/";
|
|
|
|
|
if (!file_exists($this->uploadPath . $current_path))
|
|
|
|
|
mkdir($this->uploadPath . $current_path);
|
2014-12-26 13:45:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-27 16:03:41 +00:00
|
|
|
|
if($model == self::DATETIME_MODEL_YMD || $model == self::DATETIME_MODEL_YM){
|
|
|
|
|
$current_path .= $m . "/";
|
|
|
|
|
if (!file_exists($this->uploadPath . $current_path))
|
|
|
|
|
mkdir($this->uploadPath . $current_path);
|
2014-12-26 13:45:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-27 16:03:41 +00:00
|
|
|
|
if($model == self::DATETIME_MODEL_YMD) {
|
|
|
|
|
$current_path .= $d ."/";
|
|
|
|
|
if (!file_exists($this->uploadPath . $current_path))
|
|
|
|
|
mkdir($this->uploadPath . $current_path);
|
2014-12-26 13:45:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->uploadPath .= $current_path;
|
|
|
|
|
$this->relativePath .= $current_path;
|
|
|
|
|
return $current_path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $fileName
|
|
|
|
|
* @param $fileExt
|
|
|
|
|
*/
|
2015-01-13 09:24:11 +00:00
|
|
|
|
public function setFileName($fileName,$fileExt = "")
|
2014-12-26 13:45:05 +00:00
|
|
|
|
{
|
2015-04-30 10:47:18 +00:00
|
|
|
|
$tools = $this->serviceManager->get('Tools');
|
|
|
|
|
$this->fileUuid = $uuid = $tools->uuid();
|
2015-01-13 09:24:11 +00:00
|
|
|
|
|
2015-04-30 10:47:18 +00:00
|
|
|
|
if(!empty($fileName)){
|
|
|
|
|
$this->fileName = $fileName;
|
2014-12-26 13:45:05 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-13 09:24:11 +00:00
|
|
|
|
if(empty($fileExt))
|
|
|
|
|
$this->fileName = $uuid;
|
|
|
|
|
else
|
|
|
|
|
$this->fileName = $uuid . "." . $fileExt;
|
2014-12-26 13:45:05 +00:00
|
|
|
|
|
2015-01-13 09:24:11 +00:00
|
|
|
|
return;
|
2014-12-26 13:45:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-28 15:37:16 +00:00
|
|
|
|
|
2015-01-14 08:49:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param $params
|
|
|
|
|
*/
|
2014-12-28 15:37:16 +00:00
|
|
|
|
public function setParams($params)
|
|
|
|
|
{
|
|
|
|
|
$this->params = $params;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-14 08:49:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* 强制关闭文件上传前的钩子,默认是所有上传必须执行此钩子已避免上传文件不符合规格
|
|
|
|
|
* 除了后台中特殊的文件操作之外不建议关闭
|
|
|
|
|
*/
|
|
|
|
|
public function forceDetachPreCheckTrigger()
|
|
|
|
|
{
|
|
|
|
|
$this->returnInPreCheckTrigger = false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 13:45:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getFileName()
|
|
|
|
|
{
|
|
|
|
|
return $this->fileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getUploadPath()
|
|
|
|
|
{
|
|
|
|
|
return $this->uploadPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getRelativePath()
|
|
|
|
|
{
|
|
|
|
|
return $this->relativePath;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 02:35:21 +00:00
|
|
|
|
public function setDatePathMode($mode){
|
|
|
|
|
$this->datePathMode = $mode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getDatePathMode(){
|
|
|
|
|
return $this->datePathMode;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 07:50:44 +00:00
|
|
|
|
public function getUuid(){
|
|
|
|
|
return $this->fileUuid;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 13:45:05 +00:00
|
|
|
|
}
|