牛骨文教育服务平台(让学习变的简单)
博文笔记

thinkphp框架中使用ueditor入门

创建时间:2017-06-12 投稿人: 浏览次数:155

一.获取ueditor安装包

下载ueditor源码,建议在其官网下载,官网提供的有开发版和Mini版,以及不同的编码格式, 根据个人需要下载。

Ueditor官网地址为:http://ueditor.baidu.com/website/download.html#ueditor 

二.在thinkphp3.2中的相关配置

  1. 把下载的源码解压,放在thinkphp框架的Public目录中
  2. 找到thinkphp框架系统自带类中的Html.class.php,并且添加在switch(strtoupper($type)) {}中添加代码

case "UEDITOR":

                $parseStr   =  " "."<script type="text/javascript" charset="utf-8" 

                src="__ROOT__/Public/UEditor/ueditor.config.js"></script>"." "."

                <script type="text/javascript" charset="utf-8" 

                src="__ROOT__/Public/UEditor/ueditor.all.js"></script>"." "."

                <script type="text/plain" id="".$id."" name="".$name."" style=""

                .$style."">".$content."</script>"." "."<script type="text/javascript">var ue_".$id." = UE.getEditor("".$id."");</script>"." ";

                break;

3.在项目文件的www/myproject/Application/Admin/Conf目录下新建ueditconfig.json文件,把ueditor源码的php文件夹下的config.json中的内容复制到ueditconfig.json文件中,并修改上传保存路径。(保存路径建议使用“相对于网站根目录的相对路径”,即:"/myProject/ueditor/"这样的路径,不容易发生错误)。

在需要添加百度编辑器的页面控制器中添加代码:

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]+?*//", "", file_get_contents(COMMON_PATH."Conf/ueditconfig.json")), 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(".", trim(join("",$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(".", trim(join("",$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]+?*//", "", file_get_contents(COMMON_PATH."Conf/ueditconfig.json")), 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, 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];
        }
        //倒序
        //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, $allowFiles, $files);
                } else {
                    if(in_array(".".pathinfo($file, PATHINFO_EXTENSION), $allowFiles)){


                        $files[] = array(
                            "url"=> substr($path2, strlen($_SERVER["DOCUMENT_ROOT"])),
                            "mtime"=> filemtime($path2)
                        );
                    }
                }
            }
        }
        return $files;

三.使用方法

在<head></head>中添加<taglib name="html" />

在<body></body>中添加语句<html:editor id="info" name="content" type="UEDITOR" >{$info.info}</html:editor>即可显示百度编辑器页面



声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。