码迷,mamicode.com
首页 > Web开发 > 详细

thinphp 整合ueditor

时间:2014-08-11 00:17:11      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   使用   os   

我的ueditor是部署在public/editor

部署前台页面

<script type="text/javascript" >
var UEDITOR_HOME_URL: "__PUBLIC__/ueditor/"
</script>

<script id="container" name="$des" type="text/plain">
        这里写你的初始化内容
    </script>
            
    <!-- 配置文件 -->
    <script type="text/javascript" src="__PUBLIC__/ueditor/ueditor.config.js"></script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="__PUBLIC__/ueditor/ueditor.all.js"></script>
    <!-- 实例化编辑器 -->
    <script type="text/javascript">
        var ue = UE.getEditor(‘container‘,{
            autoHeight: false,
        });
    </script>

 

修改上传配置信息

ueditor所有上传文件的配置都在config.json文件中。

上传路径修改成自己需要的

更改服务器统一入口文件

修改ueditor.config.js文件

统一入口都走public模块的editor 方法。

1 // 服务器统一请求接口路径
2  , serverUrl: "http://localhost/index.php?m=Public&a=editor"

开发一个public 公共的模块

代码我就直接用的ueditor提供的demo。

  1 public function  getConf(){
  2         $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("public/ueditor/config.json")), true);
  3         return $CONFIG;
  4     }
  5     public  function verify(){
  6         import(‘ORG.Util.Image‘);
  7         Image::buildImageVerify();
  8     }
  9     public  function upload(){
 10         import(‘ORG.Net.UploadFile‘);
 11         $upload = new UploadFile();// 实例化上传类
 12         $upload->maxSize  = 1024*1024 ;// 设置附件上传大小
 13         $upload->thumb=true;
 14         $upload->thumbMaxWidth=‘150,100‘;
 15         $upload->thumbMaxHeight=‘150,50‘;
 16         $upload->thumbPrefix="mid_,small_";
 17         $upload->autoSub=true;
 18         $upload->subType=date;
 19         $upload->dateFormat=‘Ymd‘;
 20         $upload->allowExts  = array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘);// 设置附件上传类型
 21         $upload->savePath =  ‘./Public/Uploads/‘;// 设置附件上传目录
 22         if(!$upload->upload()) {// 上传错误提示错误信息
 23             $this->error($upload->getErrorMsg());
 24         }else{// 上传成功 获取上传文件信息
 25             $info =  $upload->getUploadFileInfo();
 26             return $info;
 27         }
 28     }
 29 
 30 public function action_list(){
 31     $CONFIG=$this->getConf();
 32         /* 判断类型 */
 33 switch ($_GET[‘action‘]) {
 34     /* 列出文件 */
 35     case ‘listfile‘:
 36         $allowFiles = $CONFIG[‘fileManagerAllowFiles‘];
 37         $listSize = $CONFIG[‘fileManagerListSize‘];
 38         $path = $CONFIG[‘fileManagerListPath‘];
 39         break;
 40     /* 列出图片 */
 41     case ‘listimage‘:
 42     default:
 43         $allowFiles = $CONFIG[‘imageManagerAllowFiles‘];
 44         $listSize = $CONFIG[‘imageManagerListSize‘];
 45         $path = $CONFIG[‘imageManagerListPath‘];
 46 }
 47 $allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
 48 
 49 /* 获取参数 */
 50 $size = isset($_GET[‘size‘]) ? htmlspecialchars($_GET[‘size‘]) : $listSize;
 51 $start = isset($_GET[‘start‘]) ? htmlspecialchars($_GET[‘start‘]) : 0;
 52 $end = $start + $size;
 53 
 54 /* 获取文件列表 */
 55 $path = $_SERVER[‘DOCUMENT_ROOT‘] . (substr($path, 0, 1) == "/" ? "":"/") . $path;
 56 $files = getfiles($path, $allowFiles);
 57 if (!count($files)) {
 58     return json_encode(array(
 59         "state" => "no match file",
 60         "list" => array(),
 61         "start" => $start,
 62         "total" => count($files)
 63     ));
 64 }
 65 
 66 /* 获取指定范围的列表 */
 67 $len = count($files);
 68 for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
 69     $list[] = $files[$i];
 70 }
 71 //倒序
 72 //for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
 73 //    $list[] = $files[$i];
 74 //}
 75 
 76 /* 返回数据 */
 77 $result = json_encode(array(
 78     "state" => "SUCCESS",
 79     "list" => $list,
 80     "start" => $start,
 81     "total" => count($files)
 82 ));
 83 
 84 return $result;
 85 
 86     }
 87     public function action_upload(){
 88         import(‘ORG.Net.Uploader‘);
 89         $CONFIG=$this->getConf();
 90         /* 上传配置 */
 91         $base64 = "upload";
 92         switch (htmlspecialchars($_GET[‘action‘])) {
 93             case ‘uploadimage‘:
 94                 $config = array(
 95                     "pathFormat" => $CONFIG[‘imagePathFormat‘],
 96                     "maxSize" => $CONFIG[‘imageMaxSize‘],
 97                     "allowFiles" => $CONFIG[‘imageAllowFiles‘]
 98                 );
 99                 $fieldName = $CONFIG[‘imageFieldName‘];
100                 break;
101             case ‘uploadscrawl‘:
102                 $config = array(
103                     "pathFormat" => $CONFIG[‘scrawlPathFormat‘],
104                     "maxSize" => $CONFIG[‘scrawlMaxSize‘],
105                     "allowFiles" => $CONFIG[‘scrawlAllowFiles‘],
106                     "oriName" => "scrawl.png"
107                 );
108                 $fieldName = $CONFIG[‘scrawlFieldName‘];
109                 $base64 = "base64";
110                 break;
111             case ‘uploadvideo‘:
112                 $config = array(
113                     "pathFormat" => $CONFIG[‘videoPathFormat‘],
114                     "maxSize" => $CONFIG[‘videoMaxSize‘],
115                     "allowFiles" => $CONFIG[‘videoAllowFiles‘]
116                 );
117                 $fieldName = $CONFIG[‘videoFieldName‘];
118                 break;
119             case ‘uploadfile‘:
120             default:
121                 $config = array(
122                     "pathFormat" => $CONFIG[‘filePathFormat‘],
123                     "maxSize" => $CONFIG[‘fileMaxSize‘],
124                     "allowFiles" => $CONFIG[‘fileAllowFiles‘]
125                 );
126                 $fieldName = $CONFIG[‘fileFieldName‘];
127                 break;
128         }
129 
130         /* 生成上传实例对象并完成上传 */
131         $up = new Uploader($fieldName, $config, $base64);
132 
133         /**
134          * 得到上传文件所对应的各个参数,数组结构
135          * array(
136          *     "state" => "",          //上传状态,上传成功时必须返回"SUCCESS"
137          *     "url" => "",            //返回的地址
138          *     "title" => "",          //新文件名
139          *     "original" => "",       //原始文件名
140          *     "type" => ""            //文件类型
141          *     "size" => "",           //文件大小
142          * )
143          */
144 
145         /* 返回数据 */
146         return json_encode($up->getFileInfo());
147 
148     }
149     /**
150      * 编辑器
151      */
152     public  function editor(){
153         $CONFIG = $this->getConf();
154         $action = $_GET[‘action‘];
155         
156         switch ($action) {
157             case ‘config‘:
158                 $result =  json_encode($CONFIG);
159                 break;
160         
161                 /* 上传图片 */
162             case ‘uploadimage‘:
163                 /* 上传涂鸦 */
164             case ‘uploadscrawl‘:
165                 /* 上传视频 */
166             case ‘uploadvideo‘:
167                 /* 上传文件 */
168             case ‘uploadfile‘:
169                 $result = $this->action_upload();
170                 break;
171         
172                 /* 列出图片 */
173             case ‘listimage‘:
174                 $result =$this-> action_list();
175                 break;
176                 /* 列出文件 */
177             case ‘listfile‘:
178                 $result = $this-> action_list();
179                 break;
180         
181                 /* 抓取远程文件 */
182             case ‘catchimage‘:
183                 $result = $this-> action_crawler();
184                 break;
185         
186             default:
187                 $result = json_encode(array(
188                 ‘state‘=> ‘请求地址出错‘
189                         ));
190                         break;
191         }
192         
193         /* 输出结果 */
194         if (isset($_GET["callback"])) {
195             if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
196                 //echo htmlspecialchars($_GET["callback"]) . ‘(‘ . $result . ‘)‘;
197                 //echo ($_GET["callback"]) . ‘(‘ . $result . ‘)‘;
198                 die(($_GET["callback"]) . ‘(‘ . $result . ‘)‘);
199             } else {
200                 echo json_encode(array(
201                         ‘state‘=> ‘callback参数不合法‘
202                 ));
203             }
204         } else {
205             echo $result;
206         }
207     }
208     /**
209      * 上传抓图
210      */
211     public  function action_crawler(){
212         import(‘ORG.Net.Uploader‘);
213         $CONFIG=$this->getConf();
214         /* 上传配置 */
215         $config = array(
216                 "pathFormat" => $CONFIG[‘catcherPathFormat‘],
217                 "maxSize" => $CONFIG[‘catcherMaxSize‘],
218                 "allowFiles" => $CONFIG[‘catcherAllowFiles‘],
219                 "oriName" => "remote.png"
220         );
221         $fieldName = $CONFIG[‘catcherFieldName‘];
222         
223         /* 抓取远程图片 */
224         $list = array();
225         if (isset($_POST[$fieldName])) {
226             $source = $_POST[$fieldName];
227         } else {
228             $source = $_GET[$fieldName];
229         }
230         foreach ($source as $imgUrl) {
231             $item = new Uploader($imgUrl, $config, "remote");
232             $info = $item->getFileInfo();
233             array_push($list, array(
234             "state" => $info["state"],
235             "url" => $info["url"],
236             "size" => $info["size"],
237             "title" => htmlspecialchars($info["title"]),
238             "original" => htmlspecialchars($info["original"]),
239             "source" => htmlspecialchars($imgUrl)
240             ));
241         }
242         
243         /* 返回抓取数据 */
244         return json_encode(array(
245                 ‘state‘=> count($list) ? ‘SUCCESS‘:‘ERROR‘,
246                 ‘list‘=> $list
247         ));
248     }

控制器接收数据

要使用htmlspecialchars_decode把一些预定义的 HTML 实体转换为字符

1 $data[‘des‘]=htmlspecialchars_decode($this->_post(‘des‘));

 

thinphp 整合ueditor,布布扣,bubuko.com

thinphp 整合ueditor

标签:des   style   blog   http   color   java   使用   os   

原文地址:http://www.cnblogs.com/tl542475736/p/3903646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!