Ueditor 1.4.3.1 使用 ThinkPHP 3.2.3 的上传类进行图片上传

摘要:
在ThinkPaPHP 3.2.3中集成最新版本的百度编辑器Ueditor1.4.3.1,并将编辑器提供的上传类替换为ThinkPaPHP 3.2.3.Controller中的上传类。/Application/Admin/Controller/BlogController。班php和视图/Application/Admin/View/Log_add_博客。html是用于添加博客文章的控制器和视图--ueditorstart--˃窗口。UEDITOR_ HOME_ URL='__ROOT__/Data/UEDITOR/';窗onload=function(){window.UEDITOR_CONFIG.initialFrameWidth=750;//初始化编辑器宽度窗口。UEDITOR_CONFIG.initialFrameHeight=200;//初始化编辑高度//自定义请求地址UE.editor.protype.bkGetActionUrl=UE.Editer.prototype.getActionUrl;UE.editor.protype.getActionUrl=function{if{//上载图像返回“{:U}”;}否则{//加载配置returnthis.bkGetActionUrl.call;}}//自定义请求地址UE结束。getEditor;}⑤ 在控制器的上载方法中,仅/Data/ueditor/controller修改PHP中的方法:subName=array;//子目录创建方法//upload$info=$upload-˃upload()(

在 ThinkPHP 3.2.3 中集成百度编辑器最新版 Ueditor 1.4.3.1,同时将编辑器自带的上传类替换成 ThinkPHP 3.2.3 中的上传类。

① 下载编辑器(下载地址:http://ueditor.baidu.com/website/download.html),解压后放入项目根目录的 Data 目录并且将解压出来的目录重命名为 ueditor。

项目中的控制器 ./Application/Admin/Controller/BlogController.class.php 和 视图 ./Application/Admin/View/Blog_add_blog.html 分别是添加博客文章的控制器和视图。

② 在 Blog_add_blog.html 中引入编辑器的配置文件 ./Data/ueditor/ueditor.config.js 和 编辑器的类库文件 ./Data/ueditor/ueditor.all.min.js

③ 在 Blog_add_blog.html 中,用于填写文章的文本域:

<textarea name="content" id="content"></textarea>

因此需要在视图文件的 js 中进行设置,根据默认文本域的 id 将文本域替换成百度编辑器:

    <script>
        window.UEDITOR_HOME_URL = '__ROOT__/Data/ueditor/';
        window.onload = function() {
            UE.getEditor('content');
        }
    </script>

同时可以对编辑器的其他配置进行设置,例如:

        window.onload = function() {
            window.UEDITOR_CONFIG.initialFrameWidth = 750; //初始化编辑器宽度
            window.UEDITOR_CONFIG.initialFrameHeight = 200;  //初始化编辑器高度         
            UE.getEditor('content');
        }

④ 根据浏览器的开发者工具可以看到在上传图片时请求的地址是 ./Data/ueditor/php/controller.php,参数 action = uploadimage

Ueditor 1.4.3.1 使用 ThinkPHP 3.2.3 的上传类进行图片上传第1张

controller.php 是服务器统一请求接口路径,在 line 9 ~ line 23 中如果请求的参数 action = uploadimage 时,则

$result = include("action_upload.php");

在 action_upload.php 中包含了上传的配置选项,并且包含了 Uploader.class.php 文件

Uploader.class.php 文件是 ueditor 的上传类文件。

因此如果需要自定义上传类,只需要自定义请求地址即可(把 controller.php 替换成自己的地址),根据文档中 http://fex.baidu.com/ueditor/#qa-customurl 的说明,由于所有ueditor请求都通过editor对象的getActionUrl方法获取请求地址,可以直接通过复写这个方法实现在视图文件,只需要 ./Application/Admin/View/Blog_add_blog.html 的 js 中添加上:

            UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
            UE.Editor.prototype.getActionUrl = function(action) {
                if (action == 'uploadimage') {    //上传图片
                      return "{:U('Admin/Blog/upload',array('action'=>'uploadimage'),'')}";
                } else  if(action == 'config') {    //加载配置
                        return this._bkGetActionUrl.call(this, action);
                }
            }

视图文件完整的 js:

    <!-- ueditor start-->
    <script>
        window.UEDITOR_HOME_URL = '__ROOT__/Data/ueditor/';
        window.onload = function() {
            window.UEDITOR_CONFIG.initialFrameWidth = 750; //初始化编辑器宽度
              window.UEDITOR_CONFIG.initialFrameHeight = 200;  //初始化编辑器高度
              //自定义请求地址
            UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
            UE.Editor.prototype.getActionUrl = function(action) {
                if (action == 'uploadimage') {    //上传图片
                      return "{:U('Admin/Blog/upload',array('action'=>'uploadimage'),'')}";
                } else  if(action == 'config') {    //加载配置
                        return this._bkGetActionUrl.call(this, action);
                }
            }             
            //自定义请求地址结束
            UE.getEditor('content');
        }
    </script>
    <script src="__ROOT__/Data/ueditor/ueditor.config.js"></script>
    <script src="__ROOT__/Data/ueditor/ueditor.all.min.js"></script>
    <!--ueditor end-->

⑤ 在控制器的 upload 方法中,只需要对 ./Data/ueditor/controller.php 中的方法进行修改:

<?php
namespace AdminController;
use ThinkUpload;

class BlogController extends CommonController{
    //上传图片
    public function upload() {
        date_default_timezone_set("Asia/chongqing");
        error_reporting(E_ERROR);
        header("Content-Type: text/html; charset=utf-8");

        $CONFIG = json_decode(preg_replace("//*[sS]+?*//", "", file_get_contents("config.json")), true);
        $action = $_GET['action'];
        if('uploadimage' == $action) { //上传图片
            //处理图片上传开始
            //实例化上传类
            $upload = new Upload();
            //配置
            $upload->subName = array('date', 'Ym');//子目录创建方式
            //上传
            $info = $upload->upload();
            //p($info);//上传信息
            if($info) {
                /**
                 * 得到上传文件所对应的各个参数,数组结构
                 * array(
                 *     "state" => "",          //上传状态,上传成功时必须返回"SUCCESS"
                 *     "url" => "",            //返回的地址
                 *     "title" => "",          //新文件名
                 *     "original" => "",       //原始文件名
                 *     "type" => ""            //文件类型
                 *     "size" => "",           //文件大小
                 * )
                 */
                $arr = array(
                    'state'=>'SUCCESS',
                    'url'=>'http://'.$_SERVER['SERVER_NAME'].'/Uploads/'.$info['upfile']['savepath'].$info['upfile']['savename'],
                    'title'=>$info['upfile']['savename'],
                    'original'=>$info['upfile']['name'],
                    'type'=>$info['upfile']['ext'],
                    'size'=>$info['upfile']['size']
                );
                //print_r($arr);
                /* 返回数据 */
                $result = json_encode($arr);
            } else {
                $arr = array('state'=>$upload->getError());
            }
            //图片上传结束
        } elseif('config' == $action) { //加载配置
            $result =  json_encode($CONFIG);
        }
        /* 输出结果 */
        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;
        }
    }
}

免责声明:文章转载自《Ueditor 1.4.3.1 使用 ThinkPHP 3.2.3 的上传类进行图片上传》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇swift 枚举登陆验证前对用户名和密码加密之后传输数据---base64加密下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

2020最新编辑器集成eslint、prettier、stylelint,git提交预检查代码配置

webstorm 编辑器自动格式化配置: plugin webstorm设置搜eslintr、eslint、stylelint,如果没有,搜plugin,安装prettier、eslint、stylelint File watchs 设置里搜File watchs,增加prettier,打钩开启自动格式化,则会在保存时自动格式化。 VS cod...

第二章:Android Studio概述(一)[学习Android Studio汉化教程]

Android Studio是一个视窗化的开发环境。为了充分利用有限的屏幕空间,不让你束手束脚,Android Studio 在特定的时间仅仅显示一小部分可用窗口。  除了一些上下文敏感的窗口和上下文相关的窗口显示出来外,其他的仍旧隐藏,除非你主动打开它们。  或者相反,一些可见的窗口直到你主动隐藏它们。  为了充分利用Android Studio,你就需...

帝国CMS 复制word里面带图文的文章,图片可以直接显示

1.4.2之后官方并没有做功能的改动,1.4.2在word复制这块没有bug,其他版本会出现手动无法转存的情况 本文使用的后台是Java。前端为Jsp(前端都一样,后台如果语言不通得自己做 Base64编码解码) 因为公司业务需要支持IE8 ,网上其实有很多富文本框,效果都很好。 例如www.wangEditor.com  但试了一圈都不支持IE8 。 所...

Visual Studio Code 的使用方法和技巧

VSCode是微软推出的一款轻量编辑器,采取了和VS相同的UI界面,搭配合适的插件可以优化前端开发的体验。 布局:左侧是用于展示所要编辑的所有文件和文件夹的文件管理器,依次是`资源管理器`,`搜索`,`GIT`,`调试`,`插件`,右侧是打开文件的编辑区域,最多可同时打开三个编辑区域到侧边。 底栏:依次是`Git Branch`,`error&wa...

asp网站中使用百度ueditor教程

1、根据网站类型及编码选择相应的ueditor版本,如我的网站编码为gb2312,则选择ueditor1.43aspgbk版。2、本机IE浏览器应为8.0或以上,8.0以下的ueditor1.43不支持。3、把ueditor文件夹放于网站根目录下,在网页<head>与</head>间插入以下内容:注意src后的路径根据实际位置修改。...

前端开发编辑器(notepad++、sublime text)

1、Notepad++ 正则替换:   如<td>第三节</td>   替换成<td><input type="text" value="第三节" /></td>   可以查找:第(.)节,替换为:<input type="text" value="第(1)节" />。 2、 Note...