Thinkphp 3.2.3集成百度编辑器UEditor

前两天刚集成好,我说下我的做法吧

1、把百度编辑器放到项目的Public目录下 命名为:UEditor

2、在ThinkPHP/Library/Think/Template/Taglib 目录下编辑 Html.class.php 加上下面的代码:

        switch(strtoupper($type)) {

            case 'FCKEDITOR':

                //.......

                break;

            case 'FCKMINI':

                //.......

                break;

            case 'EWEBEDITOR':

                //.......

                break;

            case 'NETEASE':

                //.......

                break;

            case 'UBB':

                //.......

                break;

            case 'KINDEDITOR':

               //.......

                break;

            case 'UEDITOR':

                $parseStr   =  "n".'<script type="text/javascript" charset="utf-8" src="__ROOT__/Public/UEditor/ueditor.config.js"></script>'."n".'<script type="text/javascript" charset="utf-8" src="__ROOT__/Public/UEditor/ueditor.all.js"></script>'."n".'<script type="text/plain" id="'.$id.'" name="'.$name.'" .$style.'">'.$content.'</script>'."n".'<script type="text/javascript">var ue_'.$id.' = UE.getEditor("'.$id.'");</script>'."n";

                break;

                //.......

3、在需要用到编辑器的模板head内加上:

<taglib name="html" />

在用到编辑器的地方加上:

<html:editor id="info" name="info" type="UEDITOR" >{$info.info}</html:editor>

4、在百度编辑器目录的ueditor.config.js文件内修改:

        // 服务器统一请求接口路径

       ,serverUrl: URL + "../../index.php/Home/Index/ueditup"

5、就是你需要的用TP自带上传类处理上传了,在Home模块的Index控制器里加上下面的方法:

    public function ueditup(){

        header("Content-Type: text/html; charset=utf-8");

        $editconfig = json_decode(preg_replace("//*[sS]+?*//","",file_get_contents(COMMON_PATH."Conf/ueditconfig.json")),true);

            //dump($editconfig);

        $action = I('get.action');

        //echo $action;

        switch ($action) {

            case 'config':

                $result =  json_encode($editconfig);

                break;

            /* 上传图片 */

            case 'uploadimage':

                $result = $this->editup('img');

                break;

            /* 上传涂鸦 */

            case 'uploadscrawl':

                $result = $this->editup('img');

                break;

            case 'uploadvideo':

                $result = $this->editup('video');

                break;

            case 'uploadfile':

                $result = $this->editup('file');

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

                break;

            /* 列出图片 */

            case 'listimage':

                $result = $this->editlist('listimg');

                break;

            /* 列出文件 */

            case 'listfile':

                $result = $this->editlist('listfile');

                break;

            /* 抓取远程文件 */

            case 'catchimage':

                //$result = include("action_crawler.php");

                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;

        }

        

    }

    public function editup($uptype){

        if($this->islogin==false){

            $_re_data['state'] = '请登陆';

            return json_encode($_re_data);

        }

        $editconfig = json_decode(preg_replace("//*[sS]+?*//",true);

        switch ($uptype) {

            case 'img':

                $upload = new ThinkUpload();// 实例化上传类

                $upload->rootPath  =     '.';

                $upload->maxSize   =     $editconfig['imageMaxSize'];

                $upload->exts      =     explode('.',trim(join('',$editconfig['imageAllowFiles']),'.'));

                $upload->savePath  =     $editconfig['imagePathFormat'];

                $upload->saveName  =     time().rand(100000,999999);

                $info   =   $upload->uploadOne($_FILES[$editconfig['imageFieldName']]);

                break;

            case 'file':

                $upload = new ThinkUpload();// 实例化上传类

                $upload->rootPath  =     '.';

                $upload->maxSize   =     $editconfig['fileMaxSize'];

                $upload->exts      =     explode('.',$editconfig['fileAllowFiles']),'.'));

                $upload->savePath  =     $editconfig['filePathFormat'];

                $upload->saveName  =     time().rand(100000,999999);

                $info   =   $upload->uploadOne($_FILES[$editconfig['fileFieldName']]);

                break;

            case 'video':

                $upload = new ThinkUpload();// 实例化上传类

                $upload->rootPath  =     '.';

                $upload->maxSize   =     $editconfig['videoMaxSize'];

                $upload->exts      =     explode('.',$editconfig['videoAllowFiles']),'.'));

                $upload->savePath  =     $editconfig['videoPathFormat'];

                $upload->saveName  =     time().rand(100000,999999);

                $info   =   $upload->uploadOne($_FILES[$editconfig['videoFieldName']]);

                break;

            default:

                return false;

                break;

        }

        if(!$info) {// 上传错误提示错误信息

            $_re_data['state'] = $upload->getError();

            $_re_data['url'] = '';

            $_re_data['title'] = '';

            $_re_data['original'] = '';

            $_re_data['type'] = '';

            $_re_data['size'] = '';

        }else{// 上传成功 获取上传文件信息

            $_re_data['state'] = 'SUCCESS';

            $_re_data['url'] = $info['savepath'].$info['savename'];

            $_re_data['title'] = $info['savename'];

            $_re_data['original'] = $info['name'];

            $_re_data['type'] = '.'.$info['ext'];

            $_re_data['size'] = $info['size'];

        }

        return json_encode($_re_data);

    }

    public function editlist($listtype){

        $editconfig = json_decode(preg_replace("//*[sS]+?*//",true);

        switch ($listtype) {

            case 'listimg':

                $allowFiles = $editconfig['imageManagerAllowFiles'];

                $listSize = $editconfig['imageManagerListSize'];

                $path = $editconfig['imageManagerListPath'];

                break;

            

            case 'listfile':

                $allowFiles = $editconfig['fileManagerAllowFiles'];

                $listSize = $editconfig['fileManagerListSize'];

                $path = $editconfig['fileManagerListPath'];

                break;

            

            default:

                return false;

                break;

        }

        /* 获取参数 */

        $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,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];

        }

        //倒序

        //for ($i = $end,$list = array(); $i < $len && $i < $end; $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

     */

    public 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,$files);

                } else {

                    if(in_array('.'.pathinfo($file,PATHINFO_EXTENSION),$allowFiles)){

                        $files[] = array(

                            'url'=> substr($path2,strlen($_SERVER['DOCUMENT_ROOT'])),

                            'mtime'=> filemtime($path2)

                        );

                    }

                }

            }

        }

        return $files;

    }

记得把 ueditconfig.json这个文件放到配置文件目录下也就是Home/Conf/这个目录下

这个文件的内容如下:

/* 前后端通信相关的配置,注释只允许使用多行方式 */

 {

    /* 上传图片配置项 */

    "imageActionName": "uploadimage",/* 执行上传图片的action名称 */

    "imageFieldName": "upfile",/* 提交的图片表单名称 */

    "imageMaxSize": 2048000,/* 上传大小限制,单位B */

    "imageAllowFiles": [".png",".jpg",".jpeg",".gif",".bmp"],/* 上传图片格式显示 */

    "imageCompressEnable": true,/* 是否压缩图片,默认是true */

    "imageCompressBorder": 1600,/* 图片压缩最长边限制 */

    "imageInsertAlign": "none",/* 插入的图片浮动方式 */

    "imageUrlPrefix": "",/* 图片访问路径前缀 */

    /* "imagePathFormat": "/Uploads/editor/image/{time}{rand:6}",*/

    "imagePathFormat": "/Uploads/editor/image/",/* 上传保存路径,可以自定义保存路径和文件名格式 */

    /* 涂鸦图片上传配置项 */

    "scrawlActionName": "uploadscrawl",/* 执行上传涂鸦的action名称 */

    "scrawlFieldName": "upfile",/* 提交的图片表单名称 */

    "scrawlPathFormat": "/Uploads/editor/image/",可以自定义保存路径和文件名格式 */

    "scrawlMaxSize": 2048000,/* 上传大小限制,单位B */

    "scrawlUrlPrefix": "",/* 图片访问路径前缀 */

    "scrawlInsertAlign": "none",

    /* 截图工具上传 */

    "snapscreenActionName": "uploadimage",/* 执行上传截图的action名称 */

    "snapscreenPathFormat": "/Uploads/editor/image/",可以自定义保存路径和文件名格式 */

    "snapscreenUrlPrefix": "",/* 图片访问路径前缀 */

    "snapscreenInsertAlign": "none",/* 插入的图片浮动方式 */

    /* 抓取远程图片配置 */

    "catcherLocalDomain": ["127.0.0.1","localhost","img.baidu.com"],

    "catcherActionName": "catchimage",/* 执行抓取远程图片的action名称 */

    "catcherFieldName": "source",/* 提交的图片列表表单名称 */

    "catcherPathFormat": "/Uploads/editor/image/",可以自定义保存路径和文件名格式 */

    "catcherUrlPrefix": "",/* 图片访问路径前缀 */

    "catcherMaxSize": 2048000,/* 上传大小限制,单位B */

    "catcherAllowFiles": [".png",/* 抓取图片格式显示 */

    /* 上传视频配置 */

    "videoActionName": "uploadvideo",/* 执行上传视频的action名称 */

    "videoFieldName": "upfile",/* 提交的视频表单名称 */

    "videoPathFormat": "/Uploads/editor/video/",可以自定义保存路径和文件名格式 */

    "videoUrlPrefix": "",/* 视频访问路径前缀 */

    "videoMaxSize": 102400000,/* 上传大小限制,单位B,默认100MB */

    "videoAllowFiles": [

        ".flv",".swf",".mkv",".avi",".rm",".rmvb",".mpeg",".mpg",

        ".ogg",".ogv",".mov",".wmv",".mp4",".webm",".mp3",".wav",".mid"],/* 上传视频格式显示 */

    /* 上传文件配置 */

    "fileActionName": "uploadfile",/* controller里,执行上传视频的action名称 */

    "fileFieldName": "upfile",/* 提交的文件表单名称 */

    "filePathFormat": "/Uploads/editor/file/",可以自定义保存路径和文件名格式 */

    "fileUrlPrefix": "",/* 文件访问路径前缀 */

    "fileMaxSize": 51200000,/* 上传大小限制,单位B,默认50MB */

    "fileAllowFiles": [

        ".png",".bmp",

        ".flv",".mid",

        ".rar",".zip",".tar",".gz",".7z",".bz2",".cab",".iso",

        ".doc",".docx",".xls",".xlsx",".ppt",".pptx",".pdf",".txt",".md",".xml"

    ],/* 上传文件格式显示 */

    /* 列出指定目录下的图片 */

    "imageManagerActionName": "listimage",/* 执行图片管理的action名称 */

    "imageManagerListPath": "/Uploads/editor/image/",/* 指定要列出图片的目录 */

    "imageManagerListSize": 1000,/* 每次列出文件数量 */

    "imageManagerUrlPrefix": "",/* 图片访问路径前缀 */

    "imageManagerInsertAlign": "none",/* 插入的图片浮动方式 */

    "imageManagerAllowFiles": [".png",/* 列出的文件类型 */

    /* 列出指定目录下的文件 */

    "fileManagerActionName": "listfile",/* 执行文件管理的action名称 */

    "fileManagerListPath": "/Uploads/editor/file/",/* 指定要列出文件的目录 */

    "fileManagerUrlPrefix": "",/* 文件访问路径前缀 */

    "fileManagerListSize": 1000,/* 每次列出文件数量 */

    "fileManagerAllowFiles": [

        ".png",".xml"

    ] /* 列出的文件类型 */

 }

差不多就这样了,这点点东西花了我大半天来集成

完整代码下载:

点击下载

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


(1)创建数据表: CREATE TABLE IF NOT EXISTS `think_form` (   `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
组合查询的主体还是采用数组方式查询,只是加入了一些特殊的查询支持,包括字符串模式查询(_string)、复合查询(_complex)、请求字符串查询(_query),混合查询中的特殊查询每次查询只能定义一个,由于采用数组的
(1)创建模版:/App/Home/View/Form/edit.html   <FORM method=\"post\" action=\"__URL__/update\">
自定义配置文件user.php: <?php return array(    \'sex\'=>\'man\', ); config.php: <?php return array(
在一些成熟的CMS系统中,后台一般都包含一个配置中心(如织梦后台中系统设置),以方便站长在后台修改配置文件;那么这个功能是如果实现的呢?在ThinkPHP中有没有捷径可走呢?答案肯定是有的。下面大概说一下这个功能
废话不多说先上图预览下,即本博客的分页; 这个分页类是在thinkphp框架内置的分页类的基础上修改而来,原分页类的一些设计,在实际运用中感觉不是很方便;
在php中截取字符串的函数有很多,而在thinkphp中也可以直接使用php的函数,本文给大家简单的介绍thinkPHP模板中截取字符串的具体用法,希望能对各位有所帮助。
thinkphp开发图片上传,图片异步上传是目前比较方便的功能,这里我就不写css文件了,将代码写出来。
配置数据库:/app/Common/Conf/config.php 方法一: // 添加数据库配置信息 \'DB_TYPE\'   => \'mysql\',// 数据库类型
/app/Home/Controller/IndexController.class.php
(1)创建数据表: CREATE TABLE IF NOT EXISTS `think_data` (   `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
(1)控制器设置:/app/Home/Controller/IndexController.class.php <?php namespace HomeController; use ThinkController;
(1)普通模式 http://localhost/index.php?m=module&a=action&var=value m参数表示模块,a操作表示操作(模块和操作的URL参数名称是可以配置的),后面的表示其他GET参数。
入库的时候用htmlspecialchars()处理含有html代码的内容 输出的时候用htmlspecialchars_decode()处理含有html代码的内容
<?php define(\'APP_NAME\',\'app\'); define(\'APP_PATH\',\'./app/\'); define(\'APP_DEBUG\',TRUE); // 开启调试模式
(1)创建控制器中定义read方法:/App/Home/Controller/FormController.class.php public function read($id=0){
一、实现不同字段相同的查询条件 $User = M(\"User\"); // 实例化User对象 $map[\'name|title\'] = \'thinkphp\';
如果你的数据完全是内部操作写入而不是通过表单的话(也就是说可以充分信任数据的安全),那么可以直接使用add方法,如:
查询表达式的使用格式: $map[\'字段名\'] = array(\'表达式\',\'查询条件\'); 表达式不分大小写,支持的查询表达式有下面几种,分别表示的含义是:
一、使用字符串作为查询条件 $User = M(\"User\"); // 实例化User对象 $User->where(\'type=1 AND status=1\')->select();