tp5 配置 ueditor 范例

2024-03-18 03:38
文章标签 配置 tp5 范例 ueditor

本文主要是介绍tp5 配置 ueditor 范例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

<?php
namespace app\admin\controller;use app\admin\model\Uploads;
use think\Log;
use ueditor\Uploader;class Ueditor extends Base
{//配置信息public $config = ['imageActionName' => 'uploadimage','imageFieldName' => 'upfile','imageMaxSize' => 2048000,'imageAllowFiles' =>[ 0 => '.png',1 => '.jpg', 2 => '.jpeg', 3 => '.gif', 4 => '.bmp',],'imageCompressEnable' => true,'imageCompressBorder' => 1600,'imageInsertAlign' => 'none','imageUrlPrefix' => '','imagePathFormat' => '/uploads/{yyyy}{mm}{dd}/{time}{rand:6}','scrawlActionName' => 'uploadscrawl','scrawlFieldName' => 'upfile','scrawlPathFormat' => '/uploads/{yyyy}{mm}{dd}/{time}{rand:6}','scrawlMaxSize' => 2048000,'scrawlUrlPrefix' => '','scrawlInsertAlign' => 'none','snapscreenActionName' => 'uploadimage','snapscreenPathFormat' => '/uploads/{yyyy}{mm}{dd}/{time}{rand:6}','snapscreenUrlPrefix' => '','snapscreenInsertAlign' => 'none','catcherLocalDomain' =>[0 => '127.0.0.1', 1 => 'localhost', 2 => 'img.baidu.com'],'catcherActionName' => 'catchimage','catcherFieldName' => 'source','catcherPathFormat' => '/uploads/{yyyy}{mm}{dd}/{time}{rand:6}','catcherUrlPrefix' => '','catcherMaxSize' => 2048000,'catcherAllowFiles' =>[ 0 => '.png', 1 => '.jpg', 2 => '.jpeg', 3 => '.gif', 4 => '.bmp'],'videoActionName' => 'uploadvideo','videoFieldName' => 'upfile','videoPathFormat' => '/uploads/{yyyy}{mm}{dd}/{time}{rand:6}','videoUrlPrefix' => '','videoMaxSize' => 102400000,'videoAllowFiles' =>[0 => '.flv', 1 => '.swf', 2 => '.mkv', 3 => '.avi', 4 => '.rm', 5 => '.rmvb', 6 => '.mpeg', 7 => '.mpg', 8 => '.ogg', 9 => '.ogv', 10 => '.mov', 11 => '.wmv', 12 => '.mp4', 13 => '.webm', 14 => '.mp3', 15 => '.wav', 16 => '.mid'],'fileActionName' => 'uploadfile','fileFieldName' => 'upfile','filePathFormat' => '/uploads/{yyyy}{mm}{dd}/{time}{rand:6}','fileUrlPrefix' => '','fileMaxSize' => 51200000,'fileAllowFiles' =>[0 => '.png', 1 => '.jpg', 2 => '.jpeg', 3 => '.gif', 4 => '.bmp', 5 => '.flv', 6 => '.swf', 7 => '.mkv', 8 => '.avi', 9 => '.rm', 10 => '.rmvb', 11 => '.mpeg', 12 => '.mpg', 13 => '.ogg', 14 => '.ogv', 15 => '.mov', 16 => '.wmv', 17 => '.mp4', 18 => '.webm', 19 => '.mp3', 20 => '.wav', 21 => '.mid', 22 => '.rar', 23 => '.zip', 24 => '.tar', 25 => '.gz', 26 => '.7z', 27 => '.bz2', 28 => '.cab', 29 => '.iso', 30 => '.doc', 31 => '.docx', 32 => '.xls', 33 => '.xlsx', 34 => '.ppt', 35 => '.pptx', 36 => '.pdf', 37 => '.txt', 38 => '.md', 39 => '.xml'],'imageManagerActionName' => 'listimage','imageManagerListPath' => '/uploads/','imageManagerListSize' => 20,'imageManagerUrlPrefix' => '','imageManagerInsertAlign' => 'none','imageManagerAllowFiles' =>[0 => '.png', 1 => '.jpg', 2 => '.jpeg', 3 => '.gif', 4 => '.bmp'],'fileManagerActionName' => 'listfile','fileManagerListPath' => '/uploads/','fileManagerUrlPrefix' => '','fileManagerListSize' => 20,'fileManagerAllowFiles' =>[0 => '.png', 1 => '.jpg', 2 => '.jpeg', 3 => '.gif', 4 => '.bmp', 5 => '.flv', 6 => '.swf', 7 => '.mkv', 8 => '.avi', 9 => '.rm', 10 => '.rmvb', 11 => '.mpeg', 12 => '.mpg', 13 => '.ogg', 14 => '.ogv', 15 => '.mov', 16 => '.wmv', 17 => '.mp4', 18 => '.webm', 19 => '.mp3', 20 => '.wav', 21 => '.mid', 22 => '.rar', 23 => '.zip', 24 => '.tar', 25 => '.gz', 26 => '.7z', 27 => '.bz2', 28 => '.cab', 29 => '.iso', 30 => '.doc', 31 => '.docx', 32 => '.xls', 33 => '.xlsx', 34 => '.ppt', 35 => '.pptx', 36 => '.pdf', 37 => '.txt', 38 => '.md', 39 => '.xml']];//ueditor入口public function index(){error_reporting(E_ERROR);header("Content-Type: text/html; charset=utf-8");$action = trim(params('action'));switch ($action) {case 'config':$result =  json_encode(json_decode(json_encode($this->config)));break;/* 上传图片 */case 'uploadimage':/* 上传涂鸦 */case 'uploadscrawl':/* 上传视频 */case 'uploadvideo':/* 上传文件 */case 'uploadfile':$result = $this->_upload($action);break;/* 列出图片 */case 'listimage':$result = $this->_list($action);break;/* 列出文件 */case 'listfile':$result = $this->_list($action);break;/* 抓取远程文件 */case 'catchimage':$result = $this->_crawler();break;default:$result = json_encode(array('state'=> '请求地址出错'));break;}/* 输出结果 */if (isset($_GET["callback"])) {if (preg_match("/^[\w_]+$/", $_GET["callback"])) {echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';} else {echo json_encode(array('state'=> 'callback参数不合法'));}} else {echo $result;}}/*** 上传附件和上传视频* Date: 14-04-09* Time: 上午10:17*/private function _upload($action){/* 上传配置 */$base64 = "upload";switch (htmlspecialchars($action)) {case 'uploadimage':$config = ["pathFormat" => $this->config['imagePathFormat'],"maxSize" => $this->config['imageMaxSize'],"allowFiles" => $this->config['imageAllowFiles']];$fieldName = $this->config['imageFieldName'];break;case 'uploadscrawl':$config = ["pathFormat" => $this->config['scrawlPathFormat'],"maxSize" => $this->config['scrawlMaxSize'],"allowFiles" => $this->config['scrawlAllowFiles'],"oriName" => "scrawl.png"];$fieldName = $this->config['scrawlFieldName'];$base64 = "base64";break;case 'uploadvideo':$config = ["pathFormat" => $this->config['videoPathFormat'],"maxSize" => $this->config['videoMaxSize'],"allowFiles" => $this->config['videoAllowFiles']];$fieldName = $this->config['videoFieldName'];break;case 'uploadfile':default:$config = ["pathFormat" => $this->config['filePathFormat'],"maxSize" => $this->config['fileMaxSize'],"allowFiles" => $this->config['fileAllowFiles']];$fieldName = $this->config['fileFieldName'];break;}/* 生成上传实例对象并完成上传 */$up = new Uploader($fieldName, $config, $base64);/* 返回数据 */$data = $up->getFileInfo();$this->_addInfo($data);return json_encode($data);}/*** 获取已上传的文件列表    * Date: 14-04-09* Time: 上午10:17*/private function _list($action){/* 判断类型 */switch ($action) {/* 列出文件 */case 'listfile':$allowFiles = $this->config['fileManagerAllowFiles'];$listSize = $this->config['fileManagerListSize'];$path = $this->config['fileManagerListPath'];break;/* 列出图片 */case 'listimage':default:$allowFiles = $this->config['imageManagerAllowFiles'];$listSize = $this->config['imageManagerListSize'];$path = $this->config['imageManagerListPath'];}$allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);/* 获取参数 */$size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;$start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;$end = $start + $size;/* 获取文件列表 */$path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "":"/") . $path;$files = $this->_getfiles($path, $allowFiles);if (!count($files)) {return json_encode(array("state" => "no match file","list" => array(),"start" => $start,"total" => count($files)));}/* 获取指定范围的列表 */$len = count($files);for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){$list[] = $files[$i];}/* 返回数据 */$result = json_encode(array("state" => "SUCCESS","list" => $list,"start" => $start,"total" => count($files)));return $result;}/*** 遍历获取目录下的指定类型的文件* @param $path* @param array $files* @return array*/private function _getfiles($path, $allowFiles, &$files = array()){if (!is_dir($path)) return null;if(substr($path, strlen($path) - 1) != '/') $path .= '/';$handle = opendir($path);while (false !== ($file = readdir($handle))) {if ($file != '.' && $file != '..') {$path2 = $path . $file;if (is_dir($path2)) {$this->_getfiles($path2, $allowFiles, $files);} else {if (preg_match("/\.(".$allowFiles.")$/i", $file)) {$files[] = array('url'=> substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),'mtime'=> filemtime($path2));}}}}return $files;}/*** 抓取远程图片     * Date: 14-04-14* Time: 下午19:18*/private function _crawler(){set_time_limit(0);/* 上传配置 */$config = array("pathFormat" => $this->config['catcherPathFormat'],"maxSize" => $this->config['catcherMaxSize'],"allowFiles" => $this->config['catcherAllowFiles'],"oriName" => "remote.png");$fieldName = $this->config['catcherFieldName'];/* 抓取远程图片 */$list = array();if (isset($_POST[$fieldName])) {$source = $_POST[$fieldName];} else {$source = $_GET[$fieldName];}foreach ($source as $imgUrl) {$item = new Uploader($imgUrl, $config, "remote");$info = $item->getFileInfo();$data = ["state" => $info["state"],"url" => $info["url"],"size" => $info["size"],"title" => htmlspecialchars($info["title"]),"original" => htmlspecialchars($info["original"]),"source" => htmlspecialchars($imgUrl)];array_push($list, $data);$this->_addInfo($data);}/* 返回抓取数据 */return json_encode(array('state'=> count($list) ? 'SUCCESS':'ERROR','list'=> $list));}//保存文件信息private function _addInfo($info){$record = ['admin_id' => !empty($this->administrator['id'])?$this->administrator['id']:0,'extension' => !empty($info['type'])?str_replace('.','',$info['type']):'','save_name' => !empty($info['url'])?str_replace('/uploads/','',$info['url']):'','filename' => !empty($info['title'])?$info['title']:'','size' => !empty($info['size'])?$info['size']:0,'create_time' => TIMESTAMP];//数据库存入失败记录日志if(!Uploads::addInfo($record)){Log::error(__FILE__.':'.__LINE__.' 错误:'.$record['save_name'].'数据库记录失败');}}
}

 

这篇关于tp5 配置 ueditor 范例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/821084

相关文章

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关