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

相关文章

如何为Yarn配置国内源的详细教程

《如何为Yarn配置国内源的详细教程》在使用Yarn进行项目开发时,由于网络原因,直接使用官方源可能会导致下载速度慢或连接失败,配置国内源可以显著提高包的下载速度和稳定性,本文将详细介绍如何为Yarn... 目录一、查询当前使用的镜像源二、设置国内源1. 设置为淘宝镜像源2. 设置为其他国内源三、还原为官方

CentOS7更改默认SSH端口与配置指南

《CentOS7更改默认SSH端口与配置指南》SSH是Linux服务器远程管理的核心工具,其默认监听端口为22,由于端口22众所周知,这也使得服务器容易受到自动化扫描和暴力破解攻击,本文将系统性地介绍... 目录引言为什么要更改 SSH 默认端口?步骤详解:如何更改 Centos 7 的 SSH 默认端口1

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

SpringBoot多数据源配置完整指南

《SpringBoot多数据源配置完整指南》在复杂的企业应用中,经常需要连接多个数据库,SpringBoot提供了灵活的多数据源配置方式,以下是详细的实现方案,需要的朋友可以参考下... 目录一、基础多数据源配置1. 添加依赖2. 配置多个数据源3. 配置数据源Bean二、JPA多数据源配置1. 配置主数据

Spring 基于XML配置 bean管理 Bean-IOC的方法

《Spring基于XML配置bean管理Bean-IOC的方法》:本文主要介绍Spring基于XML配置bean管理Bean-IOC的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录一. spring学习的核心内容二. 基于 XML 配置 bean1. 通过类型来获取 bean2. 通过

如何使用Nginx配置将80端口重定向到443端口

《如何使用Nginx配置将80端口重定向到443端口》这篇文章主要为大家详细介绍了如何将Nginx配置为将HTTP(80端口)请求重定向到HTTPS(443端口),文中的示例代码讲解详细,有需要的小伙... 目录1. 创建或编辑Nginx配置文件2. 配置HTTP重定向到HTTPS3. 配置HTTPS服务器

SpringBoot中配置Redis连接池的完整指南

《SpringBoot中配置Redis连接池的完整指南》这篇文章主要为大家详细介绍了SpringBoot中配置Redis连接池的完整指南,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以... 目录一、添加依赖二、配置 Redis 连接池三、测试 Redis 操作四、完整示例代码(一)pom.

Linux内核参数配置与验证详细指南

《Linux内核参数配置与验证详细指南》在Linux系统运维和性能优化中,内核参数(sysctl)的配置至关重要,本文主要来聊聊如何配置与验证这些Linux内核参数,希望对大家有一定的帮助... 目录1. 引言2. 内核参数的作用3. 如何设置内核参数3.1 临时设置(重启失效)3.2 永久设置(重启仍生效

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

如何在Mac上安装并配置JDK环境变量详细步骤

《如何在Mac上安装并配置JDK环境变量详细步骤》:本文主要介绍如何在Mac上安装并配置JDK环境变量详细步骤,包括下载JDK、安装JDK、配置环境变量、验证JDK配置以及可选地设置PowerSh... 目录步骤 1:下载JDK步骤 2:安装JDK步骤 3:配置环境变量1. 编辑~/.zshrc(对于zsh