<?php
namespace common\helpers;
use yii;
use yii\base\Model;
use yii\base\Object;
use yii\web\UploadedFile;
use yii\helpers\FileHelper;
use Exception;
class UploadHelper extends Object
{
// 处理的model
public $model;
// 最大允许上传的文件大小 5Mb
public $maxSize = 5227520;
// 上传文件表单名称
public $fileInputName = 'file';
// 图片保存绝对路径
public $savePath;
// 文件访问路径前缀
public $urlPathPrefix;
// 文件后缀格式
public $extensions;
// 文件Mime 类型
public $mimeTypes;
// 上传文件处理类
private $uploadFile;
// 文件保存路径
private $filePath;
// 文件访问路径
private $urlPath;
// 文件大小
private $fileSize;
// 文件Mime类型
private $fileMime;
// 文件后缀
private $fileExtension;
// 文件名
private $fileName;
// 图片宽度
private $thumbWidth;
// 图片高度
private $thumbHeight;
// 文件MD5哈希值
private $fileMd5;
public function init()
{
if (empty($this->model) || !($this->model instanceof Model)) {
throw new Exception(Yii::t('app', 'No delivery file class passed'));
}
// 上传文件接收
$strField = $this->fileInputName;
$this->model->$strField = $this->uploadFile = UploadedFile::getInstanceByName($this->fileInputName);
if (empty($this->uploadFile)) {
throw new Exception(Yii::t('app', 'No file uploaded'));
}
// 获取上传文件的后缀格式
$extension = $this->uploadFile->extension;
$fileMime = $this->uploadFile->type;
$thumbExtensionArray = ['jpg', 'jpeg', 'gif', 'png', 'bmp'];
$this->thumbWidth = $this->thumbHeight = 0;
// 判断上传的文件是否是图片文件
if(in_array($extension, $thumbExtensionArray)){
$this->savePath = Yii::getAlias('@frontend/web/uploads/material/images');
$this->urlPathPrefix = '/uploads/material/images';
$this->extensions = $thumbExtensionArray;
$this->mimeTypes = ['image/jpeg', 'image/bmp', 'image/gif', 'image/png', 'image/pjpeg', 'image/x-png'];
$thumbInfo = getimagesize($this->uploadFile->tempName);
$this->thumbWidth = isset($thumbInfo[0]) ? $thumbInfo[0] : $this->thumbWidth;
$this->thumbHeight = isset($thumbInfo[1]) ? $thumbInfo[1] : $this->thumbHeight;
} else {
$this->savePath = Yii::getAlias('@frontend/web/uploads/material/files');
$this->urlPathPrefix = '/uploads/material/files';
$this->extensions = ['txt', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf'];
$this->mimeTypes = ['text/plain', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/pdf'];
}
// 验证文件类型是否在允许的范围
if(!in_array($extension, $this->extensions) || !in_array($fileMime, $this->mimeTypes)){
throw new Exception(Yii::t('app', 'This file type does not allow uploading'));
}
// 目录不存在创建
$this->savePath = rtrim($this->savePath, '/');
if (!file_exists($this->savePath) && !FileHelper::createDirectory($this->savePath)) {
throw new Exception(Yii::t('app', 'No permission to create') . $this->savePath);
}
// 限制上传的文件小于5M
if($this->uploadFile->size > $this->maxSize){
throw new Exception(Yii::t('app', 'Uploaded files cannot exceed') . ($this->maxSize / 1048576) . 'Mb');
}
// 上传文件验证
if (!$this->model->validate()) {
throw new Exception($this->model->getFirstError($strField));
}
list($path, $datePath) = $this->createDir($this->savePath);
// 文件名命名
$fileName = time() . mt_rand(1000, 9999);
$this->filePath = $path . $fileName . '.' . $this->uploadFile->getExtension();
$this->urlPath = rtrim($this->urlPathPrefix, '/') . '/' . $datePath . '/' . $fileName . '.' . $this->uploadFile->getExtension();
$this->fileExtension = $extension;
$this->fileSize = round($this->uploadFile->size / 1024, 2) . ' Kb';
$this->fileMime = $this->uploadFile->type;
$this->fileName = $fileName;
$this->fileMd5 = hash_file('md5', $this->uploadFile->tempName);
}
/**
* 根据时间创建目录
*/
public function createDir($path)
{
$datePath = date('Y') . '/' . date('md');
$path = $path . '/' . $datePath . '/';
if (!file_exists($path)) {
FileHelper::createDirectory($path);
}
return [$path, $datePath];
}
/**
* 获取URL路径
* @return mixed
*/
public function getUrlPath()
{
return $this->urlPath;
}
/**
* 获取文件名
* @return mixed
*/
public function getFileName()
{
return $this->fileName;
}
/**
* 获取文件大小
* @return mixed
*/
public function getFileSize()
{
return $this->fileSize;
}
/**
* 获取文件类型
* @return mixed
*/
public function getFileType()
{
return $this->fileExtension;
}
/**
* 获取文件的Mine类型
* @return mixed
*/
public function getFileMime()
{
return $this->fileMime;
}
public function getFileMd5()
{
return $this->fileMd5;
}
/**
* 获取图片的宽度
* @return mixed
*/
public function getThumbWidth()
{
return $this->thumbWidth;
}
/**
* 获取图片的高度
* @return mixed
*/
public function getThumbHeight()
{
return $this->thumbHeight;
}
public function save()
{
return $this->uploadFile->saveAs($this->filePath);
}
}
评论