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

相关文章

mybatis映射器配置小结

《mybatis映射器配置小结》本文详解MyBatis映射器配置,重点讲解字段映射的三种解决方案(别名、自动驼峰映射、resultMap),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定... 目录select中字段的映射问题使用SQL语句中的别名功能使用mapUnderscoreToCame

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

MySQL8 密码强度评估与配置详解

《MySQL8密码强度评估与配置详解》MySQL8默认启用密码强度插件,实施MEDIUM策略(长度8、含数字/字母/特殊字符),支持动态调整与配置文件设置,推荐使用STRONG策略并定期更新密码以提... 目录一、mysql 8 密码强度评估机制1.核心插件:validate_password2.密码策略级

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

QT Creator配置Kit的实现示例

《QTCreator配置Kit的实现示例》本文主要介绍了使用Qt5.12.12与VS2022时,因MSVC编译器版本不匹配及WindowsSDK缺失导致配置错误的问题解决,感兴趣的可以了解一下... 目录0、背景:qt5.12.12+vs2022一、症状:二、原因:(可以跳过,直奔后面的解决方法)三、解决方

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Nginx中配置使用非默认80端口进行服务的完整指南

《Nginx中配置使用非默认80端口进行服务的完整指南》在实际生产环境中,我们经常需要将Nginx配置在其他端口上运行,本文将详细介绍如何在Nginx中配置使用非默认端口进行服务,希望对大家有所帮助... 目录一、为什么需要使用非默认端口二、配置Nginx使用非默认端口的基本方法2.1 修改listen指令