$store
$store : array
文件库管理 Class Upload
$view : \think\View
$request : \think\Request
__construct(\think\Request $request = null)
构造方法
\think\Request | $request | Request 对象 |
validate(array $data, string|array $validate, array $message = array(), boolean $batch = false, mixed $callback = null) : array|string|true
验证数据
array | $data | 数据 |
string|array | $validate | 验证器名或者验证规则数组 |
array | $message | 提示信息 |
boolean | $batch | 是否批量验证 |
mixed | $callback | 回调方法(闭包) |
addUploadFile( $group_id, $fileName, $fileInfo, $fileType) : \app\store\model\UploadFile
添加文件库上传记录
$group_id | ||
$fileName | ||
$fileInfo | ||
$fileType |
<?php
namespace app\store\controller;
use app\store\model\UploadFile;
use app\common\library\storage\Driver as StorageDriver;
use app\store\model\Setting as SettingModel;
/**
* 文件库管理
* Class Upload
* @package app\store\controller
*/
class Upload extends Controller
{
private $config;
/**
* 构造方法
* @throws \app\common\exception\BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function _initialize()
{
parent::_initialize();
// 存储配置信息
$this->config = SettingModel::getItem('storage');
}
/**
* 图片上传接口
* @param int $group_id
* @return array
* @throws \think\Exception
*/
public function image($group_id = -1)
{
// 实例化存储驱动
$StorageDriver = new StorageDriver($this->config);
// 设置上传文件的信息
$StorageDriver->setUploadFile('iFile');
// 上传图片
if (!$StorageDriver->upload()) {
return json(['code' => 0, 'msg' => '上传失败' . $StorageDriver->getError()]);
}
// 图片上传路径
$fileName = $StorageDriver->getFileName();
// 图片信息
$fileInfo = $StorageDriver->getFileInfo();
$ex_type =explode('/', $fileInfo['type']);
$filetype = $ex_type[0];
// 添加文件库记录
$uploadFile = $this->addUploadFile($group_id, $fileName, $fileInfo, $filetype);
// 图片上传成功
return json(['code' => 1, 'msg' => '上传成功', 'data' => $uploadFile]);
}
/**
* 添加文件库上传记录
* @param $group_id
* @param $fileName
* @param $fileInfo
* @param $fileType
* @return UploadFile
*/
private function addUploadFile($group_id, $fileName, $fileInfo, $fileType)
{
// 存储引擎
$storage = $this->config['default'];
// 存储域名
$fileUrl = isset($this->config['engine'][$storage]['domain'])
? $this->config['engine'][$storage]['domain'] : '';
// 添加文件库记录
$model = new UploadFile;
$model->add([
'group_id' => $group_id > 0 ? (int)$group_id : 0,
'storage' => $storage,
'file_url' => $fileUrl,
'file_name' => $fileName,
'file_size' => $fileInfo['size'],
'file_type' => $fileType,
'extension' => pathinfo($fileInfo['name'], PATHINFO_EXTENSION),
]);
return $model;
}
}