add Westdc\File namespace,add uuid maker in Tools Service

This commit is contained in:
Jianxuan Li 2014-12-26 21:45:05 +08:00
parent a68a573cb9
commit 08d0192a60
6 changed files with 359 additions and 11 deletions

44
Westdc/File/File.php Normal file
View File

@ -0,0 +1,44 @@
<?php
/**
* Created by PhpStorm.
* User: liujin834
* Date: 14/12/26
* Time: 下午3:52
*/
namespace Westdc\File;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
class File implements ServiceManagerAwareInterface{
protected $serviceManager;
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
return $this;
}
//获取文件扩展名
public function getFileTextExt($file_name)
{
$temp_arr = explode(".", $file_name);
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
return $file_ext;
}
//获取文件Mime通过finfo扩展
public function getFileMime($file_name)
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$filetype = finfo_file($finfo, $file_name) ; //文件mime类型
finfo_close($finfo);
return $filetype;
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace Westdc\File\Listener;
use Zend\EventManager\EventCollection;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\EventManager\EventManagerInterface;
class DefaultFileListener implements ListenerAggregateInterface
{
protected $listeners = array();
function __construct()
{
}
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach('upload.pre', function($e){
return true;
return ['error' => '文件格式不在可上传的范围内'];
}, 100);
$this->listeners[] = $events->attach('upload.pre', function($e){
return true;
return ['error' => '文件大小超出了限制'];
}, 80);
}
public function detach(EventManagerInterface $events)
{
foreach ($this->listeners as $index => $listener) {
if ($events->detach($listener)) {
unset($this->listeners[$index]);
}
}
}
}

233
Westdc/File/Upload.php Normal file
View File

@ -0,0 +1,233 @@
<?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;
}
}

View File

@ -114,15 +114,17 @@ class Theme
'/js/lib/jquery.masonry.min.js'
),
),
'uploadify' => array(
$this->ScriptKey => array(
'/js/lib/uploadify/jquery.uploadify.min.js'
),
$this->CSSKey => array(
'/js/lib/uploadify/uploadify.css'
),
),
'jquery-fileupload' => array(
$this->ScriptKey => array(
'/static/lib/jquery-fileupload/vendor/jquery.ui.widget.js',
'/static/lib/jquery-fileupload/jquery.iframe-transport.js',
'/static/lib/jquery-fileupload/jquery.fileupload.js',
),
$this->CSSKey => array(
'/static/lib/jquery-fileupload/css/jquery.fileupload.css',
)
),
'datepicker' => array(
$this->ScriptKey => array(

View File

@ -54,6 +54,32 @@ class Tools {
return $response;
}
/**
* @return string
*/
public function uuid()
{
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
}

View File

@ -108,13 +108,16 @@ class Reference extends AbstractEventManager implements ServiceManagerAwareInter
//上传文献PDF
public function uploadReferencePdf($file,$autoread = false)
{
$files = new Files();
$file_info = $files->upload($file,'literature/',true);
$fileService = $this->serviceManager->get('File/Upload');
$file_info = $fileService->upload($file,'literature/',true);
if(isset($file_info['error']) && !empty($file_info['error']))
{
return array("error" => $file_info['error']);
}
var_dump($file_info);
exit();
$file_data = array(
'filename' => $file_info['file_url'],