文件

246 行
8.5 KiB
PHP

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use app\common\exception\UploadException;
/**
* 百度编辑器上传
* Class Upload
* @package app\admin\controller
*/
class Upload extends Backend
{
// 编辑器初始配置
private $config
= [
/* 上传图片配置项 */
'imageActionName' => 'uploadimage',
'imageFieldName' => 'upfilesss',
'imageMaxSize' => 10485760, /* 上传大小限制,单位B */
'imageAllowFiles' => ['.png', '.jpg', '.jpeg', '.gif', '.bmp'],
'imageCompressEnable' => false, // 图片压缩功能true开启,false关闭
'imageCompressBorder' => 1600,
'imageInsertAlign' => 'none',
'imageUrlPrefix' => '',
'imagePathFormat' => '',
/* 涂鸦图片上传配置项 */
'scrawlActionName' => 'uploadscrawl',
'scrawlFieldName' => 'upfile',
'scrawlPathFormat' => '',
'scrawlMaxSize' => 0,
'scrawlUrlPrefix' => '',
'scrawlInsertAlign' => 'none',
/* 截图工具上传 */
'snapscreenActionName' => 'uploadimage',
'snapscreenPathFormat' => '',
'snapscreenUrlPrefix' => '',
'snapscreenInsertAlign' => 'none',
/* 抓取远程图片配置 */
'catcherLocalDomain' => ['127.0.0.1', 'localhost', 'img.baidu.com'],
'catcherActionName' => 'catchimage',
'catcherFieldName' => 'source',
'catcherPathFormat' => '',
'catcherUrlPrefix' => '',
'catcherMaxSize' => 0,
'catcherAllowFiles' => ['.png', '.jpg', '.jpeg', '.gif', '.bmp'],
/* 上传视频配置 */
'videoActionName' => 'uploadvideo',
'videoFieldName' => 'upfile',
'videoPathFormat' => '',
'videoUrlPrefix' => '',
'videoMaxSize' => 0,
'videoAllowFiles' => [".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"],
/* 上传文件配置 */
'fileActionName' => 'uploadfile',
'fileFieldName' => 'upfile',
'filePathFormat' => '',
'fileUrlPrefix' => '',
'fileMaxSize' => 20971520,
'fileAllowFiles' => [".doc", ".docx", ".ppt", ".txt", ".xls", ".xlsx", ".pdf", ".rar", ".zip"],
/* 列出指定目录下的图片 */
'imageManagerActionName' => 'listimage',
'imageManagerListPath' => '',
'imageManagerListSize' => 20,
'imageManagerUrlPrefix' => '',
'imageManagerInsertAlign' => 'none',
'imageManagerAllowFiles' => ['.png', '.jpg', '.jpeg', '.gif', '.bmp'],
/* 列出指定目录下的文件 */
'fileManagerActionName' => 'listfile',
'fileManagerListPath' => '',
'fileManagerUrlPrefix' => '',
'fileManagerListSize' => '',
'fileManagerAllowFiles' => [".flv", ".swf",],
];
/**
* 上传视频
* @return \think\response\Json
*/
public function uploadVideo()
{
if (!request()->isPost()) {
return json(['code' => 4011, 'msg' => '请求方式不正确']);
}
if (empty($_FILES)) {
return json([
'code' => 401,
'msg' => '请您上传视频'
]);
}
// 开始上传
$attachment = null;
$file = $this->request->file('video');
try {
$upload = new \app\common\library\Upload($file);
$attachment = $upload->upload();
} catch (UploadException $e) {
return json([
'code' => 402,
'msg' => $e->getMessage()
]);
}
return json([
'code' => 200,
'msg' => '上传成功',
'data' => [
'name' => $attachment->filename,
'video' => $attachment->url
]
]);
}
// 百度编辑器上传配置
public function ueditorUpload()
{
$action = input('get.action');
if (empty($action)) {
$this->error('非法操作。');
}
$result = [];
switch ($action) {
// 加载上传配置
case 'config' :
$result = $this->config;
break;
// 上传涂鸦
case 'uploadscrawl' :
break;
// 上传图片
case 'uploadimage' :
// 上传验证
$msg = $this->checkUpload($_FILES['upfilesss']['error']);
if ($msg !== 'success') {
die(json_encode([
'state' => $msg
]));
}
// 上传图片、获取图片大小和后缀
$file = request()->file('upfilesss');
$fileName = $_FILES['upfilesss']['name'];
$fileExt = explode('/', $_FILES['upfilesss']['type'])[1];
$fileSize = $_FILES['upfilesss']['size'];
// 验证图片格式是否合法
if (!in_array('.' . $fileExt, $this->config['imageAllowFiles'])) {
die(json_encode([
'state' => '您上传的图片格式不正确'
]));
}
// 超过10M无法上传
if ($fileSize > $this->config['imageMaxSize']) {
die(json_encode([
'state' => '图片大小不能超过10M'
]));
}
// 开始上传
$attachment = null;
$file = $this->request->file('upfilesss');
try {
$upload = new \app\common\library\Upload($file);
$attachment = $upload->upload();
// 获取图片名字作为alt和title
$title = substr($fileName, 0, strrpos($fileName, '.'));
return json([
'state' => 'SUCCESS',
'original' => $title,
'title' => $title,
'url' => config('wxapp.domain') . $attachment->url
]);
} catch (UploadException $e) {
return json([
'state' => $file->getError(),
'msg' => $e->getMessage()
]);
}
break;
}
die(json_encode($result));
}
/**
* 上传图片
* @return \think\response\Json
*/
public function uploadImg()
{
if (!request()->isPost()) {
return json(['code' => 4011, 'msg' => '请求方式不正确']);
}
if (empty($_FILES)) {
return json([
'code' => 401,
'msg' => '请您上传图片'
]);
}
// 开始上传
$attachment = null;
$file = $this->request->file('image');
try {
$upload = new \app\common\library\Upload($file);
$attachment = $upload->upload();
} catch (UploadException $e) {
return json([
'code' => 402,
'msg' => $e->getMessage()
]);
}
return json([
'code' => 200,
'msg' => '上传成功',
'data' => [
'name' => $attachment->filename,
'url' => $attachment->url
]
]);
}
// 上传判断
private function checkUpload($code)
{
$codeArr = [
'success',
'上传文件超出php配置upload_max_filesize限制',
'上传文件超出html表单限制',
'文件只有部分被上传',
'没有上传文件',
'没有找不到临时文件夹',
'文件写入失败,请检查权限',
'php文件上传扩展file没有打开'
];
return $codeArr[$code];
}
}