westdc-core/Westdc/File/Upload.php

223 lines
5.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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\DefaultFileUploadListener;
class Upload extends AbstractEventManager implements ServiceManagerAwareInterface{
protected $serviceManager;
private $uploadPath = "";
private $relativePath = "";
private $fileName = "";
private $config;
const DATETIME_MODEL_YMD = "Y/M/D/";
const DATETIME_MODEL_YM = "Y/M/";
const DATETIME_MODEL_Y = "Y/";
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
$this->init();
return $this;
}
public function init()
{
$Listener = new DefaultFileUploadListener;
$this->getEventManager()->attachAggregate($Listener);
$configService = $this->serviceManager->get('ConfigService');
$this->config = $configService->get('file.php');
}
public function upload($files,$rootDir = "",$childDir = "",$fileName = "",$dateDirModel = self::DATETIME_MODEL_YMD)
{
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'));
$cache_data = $results->last();
if($cache_data !== true)
{
return $cache_data;
}
$fileService = $this->serviceManager->get('File');
$this->setRootDir($rootDir);
$this->setChildDir($childDir);
if($dateDirModel !== false)
$this->makeDateDir($dateDirModel);
$this->setFileName($fileName , $fileService->getFileTextExt($files['name']));
//移动文件
$file_path = $this->getUploadPath() . $this->getFileName();
if (move_uploaded_file($file['tmp_name'], $file_path) === false) {
return array("error"=>"上传失败,请重试");
}
$file_data = array();
$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']);
$file_data['file_type'] = $fileService->getFileMime($file_path);
$results = $this->getEventManager()->trigger('upload.after', $this, compact('file_data'));
$cache_data = $results->last();
if(is_array($cache_data))
$file_data = array_merge($file_data , $cache_data);
return $file_data;
}//文件上传
/**
* 设置上传文件的根路径这样操作会忽略config中的定义
* 根路径需要自行创建
* 路径结尾必须加 "/"
* @param string $path
* @return bool
*/
public function setRootDir($path = "")
{
if(empty($path))
$this->uploadPath = $this->config->upload;
$this->uploadPath = $path;
if(!preg_match("/[\/|\\\]+$/",$this->uploadPath))
$this->uploadPath .= "/";
return true;
}
/**
* 设置子路径,自动加在根路径之后
* 如果不存在程序将创建
* @param string $dirname
* @return bool | string
*/
public function setChildDir($dirname)
{
if(empty($dirname)) {
return true;
}
$this->uploadPath .= $dirname;
if (!preg_match("/[\/|\\\]+$/", $this->uploadPath))
$this->uploadPath .= "/";
if(!file_exists($this->uploadPath)) {
if(!mkdir($this->uploadPath))
return "failed to create folder :".$this->uploadPath;
}
$this->relativePath = $dirname;
}
/**
* 创建并返回年月日的子目录路径
* @return string
*/
public function makeDateDir($model)
{
$y = date("Y");
$m = date("m");
$d = date("d");
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);
}
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);
}
if($model == self::DATETIME_MODEL_YMD) {
$current_path .= $d ."/";
if (!file_exists($this->uploadPath . $current_path))
mkdir($this->uploadPath . $current_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;
}
}