233 lines
5.5 KiB
PHP
233 lines
5.5 KiB
PHP
|
<?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;
|
|||
|
use Westdc\File\Listener\DefaultFileListener;
|
|||
|
|
|||
|
class Upload extends AbstractEventManager implements ServiceManagerAwareInterface{
|
|||
|
|
|||
|
protected $serviceManager;
|
|||
|
|
|||
|
private $uploadPath = "";
|
|||
|
private $relativePath = "";
|
|||
|
private $fileName = "";
|
|||
|
private $config;
|
|||
|
|
|||
|
public function setServiceManager(ServiceManager $serviceManager)
|
|||
|
{
|
|||
|
$this->serviceManager = $serviceManager;
|
|||
|
|
|||
|
$this->init();
|
|||
|
|
|||
|
return $this;
|
|||
|
}
|
|||
|
|
|||
|
public function init()
|
|||
|
{
|
|||
|
$Listener = new DefaultFileListener;
|
|||
|
$this->getEventManager()->attachAggregate($Listener);
|
|||
|
|
|||
|
$configService = $this->serviceManager->get('ConfigService');
|
|||
|
$this->config = $configService->get('file.php');
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* upload
|
|||
|
*
|
|||
|
* 文件上传
|
|||
|
*
|
|||
|
* @param Array $files e.g. $_FILES['Filedata']
|
|||
|
*
|
|||
|
* @return Array $msg e.g. if($msg['error'])
|
|||
|
*/
|
|||
|
public function upload($files)
|
|||
|
{
|
|||
|
if (empty($files) !== false) {
|
|||
|
return array("error"=>"请选择要上传的文件.");
|
|||
|
}
|
|||
|
|
|||
|
if (is_uploaded_file($files['tmp_name']) === false) {
|
|||
|
return array("error"=>"文件上传失败,请重新上传");
|
|||
|
}
|
|||
|
|
|||
|
$source = $this->source;
|
|||
|
$file = $files;
|
|||
|
|
|||
|
$results = $this->getEventManager()->trigger('upload.pre', $this, compact('file'));
|
|||
|
$cache_data = $results->last();
|
|||
|
|
|||
|
if($cache_data !== true)
|
|||
|
{
|
|||
|
return $cache_data;
|
|||
|
}
|
|||
|
|
|||
|
$msg = array();
|
|||
|
|
|||
|
$file_name = $files['name']; //原文件名
|
|||
|
$file_size = $files['size']; //文件大小
|
|||
|
|
|||
|
$results = $this->makeUploadTarget();
|
|||
|
|
|||
|
if(isset($results['error']))
|
|||
|
{
|
|||
|
return $results;
|
|||
|
}//文件夹问题
|
|||
|
|
|||
|
$new_file_basename = $this->gen_uuid();
|
|||
|
$file_ext = $this->getFileTextExt($file_name);
|
|||
|
$new_file_name = $new_file_basename . '.' . $file_ext;//新文件名
|
|||
|
|
|||
|
//移动文件
|
|||
|
$file_path = $results['save_path'] . $new_file_name ;
|
|||
|
|
|||
|
if (move_uploaded_file($file['tmp_name'], $file_path) === false) {
|
|||
|
return array("error"=>"上传失败,请重试");
|
|||
|
}
|
|||
|
|
|||
|
$dbsave = $db_path = $results['dbsave'];
|
|||
|
$dbsave .= $new_file_name;//数据库最终存储的文件
|
|||
|
$file_url = $dbsave;//文件链接
|
|||
|
|
|||
|
$results = $this->getEventManager()->trigger('upload.makeThumb', $this, compact('conf','file_path','db_path','file_ext','new_file_basename'));
|
|||
|
$thumbnail = $results->last();
|
|||
|
|
|||
|
$msg['file_url'] = $file_url;
|
|||
|
$msg['file_size'] = $file_size;
|
|||
|
$msg['db_path'] = $dbsave;
|
|||
|
$msg['realname'] = $file_name;
|
|||
|
$msg['file_ext'] = $file_ext;
|
|||
|
$msg['file_type'] = $this->getFileMime($file_path);
|
|||
|
$msg['thumb'] = $thumbnail;
|
|||
|
|
|||
|
return $msg;
|
|||
|
}//文件上传
|
|||
|
|
|||
|
/**
|
|||
|
* 设置上传文件的根路径,这样操作会忽略config中的定义
|
|||
|
* 根路径需要自行创建
|
|||
|
* 路径结尾必须加 "/"
|
|||
|
* @param string $path
|
|||
|
*/
|
|||
|
public function setRootDir($path = "")
|
|||
|
{
|
|||
|
if(empty($path))
|
|||
|
$this->uploadPath = $this->config->upload;
|
|||
|
|
|||
|
$this->uploadPath = $path;
|
|||
|
|
|||
|
if(!preg_match("/[\/|\\\]+$/",$this->uploadPath))
|
|||
|
$this->uploadPath .= "/";
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 设置子路径,自动加在根路径之后
|
|||
|
* 如果不存在程序将创建
|
|||
|
* @param $dirname
|
|||
|
*/
|
|||
|
public function setChildDir($dirname)
|
|||
|
{
|
|||
|
if(empty($dirname)) {
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
$this->uploadPath .= $dirname;
|
|||
|
|
|||
|
if (!preg_match("/[\/|\\\]+$/", $this->uploadPath))
|
|||
|
$this->uploadPath .= "/";
|
|||
|
|
|||
|
|
|||
|
if(!file_exists($this->uploadPath))
|
|||
|
mkdir($this->uploadPath);
|
|||
|
|
|||
|
$this->relativePath = $dirname;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 创建并返回年月日的子目录路径
|
|||
|
* @return string
|
|||
|
*/
|
|||
|
public function makeDateDir()
|
|||
|
{
|
|||
|
$y = date("Y");
|
|||
|
$m = date("m");
|
|||
|
$d = date("d");
|
|||
|
|
|||
|
$current_path = $y . "/";
|
|||
|
|
|||
|
$save_path = $this->uploadPath . $current_path;
|
|||
|
|
|||
|
if (!file_exists($save_path)) {
|
|||
|
mkdir($save_path);
|
|||
|
}
|
|||
|
|
|||
|
$current_path .= $m . "/";
|
|||
|
$save_path .= $current_path;
|
|||
|
|
|||
|
if (!file_exists($save_path)) {
|
|||
|
mkdir($save_path);
|
|||
|
}
|
|||
|
|
|||
|
$current_path .= $d ."/";
|
|||
|
$save_path .= $current_path;
|
|||
|
|
|||
|
if (!file_exists($save_path)) {
|
|||
|
mkdir($save_path);
|
|||
|
}
|
|||
|
|
|||
|
$this->uploadPath .= $current_path;
|
|||
|
$this->relativePath .= $current_path;
|
|||
|
return $current_path;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @param $fileName
|
|||
|
* @param $fileExt
|
|||
|
*/
|
|||
|
public function setFileName($fileName,$fileExt)
|
|||
|
{
|
|||
|
if(!empty($fileName)){
|
|||
|
$this->fileName = $fileName . "." .$fileExt;
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
$tools = $this->serviceManager->get('Tools');
|
|||
|
$uuid = $tools->uuid();
|
|||
|
|
|||
|
$this->fileName = $uuid . "." . $fileExt;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @return string
|
|||
|
*/
|
|||
|
public function getFileName()
|
|||
|
{
|
|||
|
return $this->fileName;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @return string
|
|||
|
*/
|
|||
|
public function getUploadPath()
|
|||
|
{
|
|||
|
return $this->uploadPath;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @return string
|
|||
|
*/
|
|||
|
public function getRelativePath()
|
|||
|
{
|
|||
|
return $this->relativePath;
|
|||
|
}
|
|||
|
|
|||
|
}
|