lukeme 发表于 2011-5-17 17:50:22

CI2.0中整合kindeditor

本帖最后由 lukeme 于 2011-5-17 17:49 编辑

在论坛中看了些整合kindeditor的帖子,发现kindeditor路径的设置,困扰了很多人,今天发个library,方便大家使用。
1、创建library
在ci2\application\libraries 下创建kindeditor.php 代码如下:
<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Kindeditor {

    private $save_path;
    private $save_url;
    private $ext_arr;
    private $max_size;

    public function __construct() {
      $this->save_path = FCPATH . 'uploadfile/';
      $this->save_url = base_url() . 'uploadfile/';
      $this->ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
      $this->max_size = 1000000;
    }

    public function upload($file) {
      if (empty($file) === FALSE) {
            //原文件名
            $file_name = $file['imgFile']['name'];
            //服务器上临时文件名
            $tmp_name = $file['imgFile']['tmp_name'];
            //文件大小
            $file_size = $file['imgFile']['size'];
            //检查文件名
            if (!$file_name) {
                $this->alert("请选择文件。");
            }
            //检查目录
            if (@is_dir($this->save_path) === false) {
                $this->alert("上传目录不存在。");
            }
            //检查目录写权限
            if (@is_writable($this->save_path) === false) {
                $this->alert("上传目录没有写权限。");
            }
            //检查是否已上传
            if (@is_uploaded_file($tmp_name) === false) {
                $this->alert("临时文件可能不是上传文件。");
            }
            //检查文件大小
            if ($file_size > $this->max_size) {
                $this->alert("上传文件大小超过限制。");
            }
            //获得文件扩展名
            $temp_arr = explode(".", $file_name);
            $file_ext = array_pop($temp_arr);
            $file_ext = trim($file_ext);
            $file_ext = strtolower($file_ext);
            //检查扩展名
            if (in_array($file_ext, $this->ext_arr) === false) {
                $this->alert("上传文件扩展名是不允许的扩展名。");
            }
            //新文件名
            $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
            //移动文件
            $file_path = $this->save_path . $new_file_name;
            if (move_uploaded_file($tmp_name, $file_path) === false) {
                $this->alert("上传文件失败。");
            }
            @chmod($file_path, 0644);
            $file_url = $this->save_url . $new_file_name;

            header('Content-type: text/html; charset=UTF-8');

            echo json_encode(array('error' => 0, 'url' => $file_url));
            exit;
      }
    }

    public function alert($msg) {
      header('Content-type: text/html; charset=UTF-8');
      echo json_encode(array('error' => 1, 'message' => $msg));
      exit;
    }

    public function manage($path) {
      //根据path参数,设置各路径和URL
      if (empty($path)) {
            $current_path = realpath($this->save_path) . '/';
            $current_url = $this->save_url;
            $current_dir_path = '';
            $moveup_dir_path = '';
      } else {
            $current_path = realpath($this->save_path) . '/' . $path;
            $current_url = $this->save_url . $path;
            $current_dir_path = $path;
            $moveup_dir_path = preg_replace('/(.*?)[^\/]+\/$/', '$1', $current_dir_path);
      }
      //排序形式,name or size or type
      $order = empty($_GET['order']) ? 'name' : strtolower($_GET['order']);

//不允许使用..移动到上一级目录
      if (preg_match('/\.\./', $current_path)) {
            echo 'Access is not allowed.';
            exit;
      }
//最后一个字符不是/
      if (!preg_match('/\/$/', $current_path)) {
            echo 'Parameter is not valid.';
            exit;
      }
//目录不存在或不是目录
      if (!file_exists($current_path) || !is_dir($current_path)) {
            echo 'Directory does not exist.';
            exit;
      }

//遍历目录取得文件信息
      $file_list = array();
      if ($handle = opendir($current_path)) {
            $i = 0;
            while (false !== ($filename = readdir($handle))) {
                if ($filename{0} == '.')
                  continue;
                $file = $current_path . $filename;
                if (is_dir($file)) {
                  $file_list[$i]['is_dir'] = true; //是否文件夹
                  $file_list[$i]['has_file'] = (count(scandir($file)) > 2); //文件夹是否包含文件
                  $file_list[$i]['filesize'] = 0; //文件大小
                  $file_list[$i]['is_photo'] = false; //是否图片
                  $file_list[$i]['filetype'] = ''; //文件类别,用扩展名判断
                } else {
                  $file_list[$i]['is_dir'] = false;
                  $file_list[$i]['has_file'] = false;
                  $file_list[$i]['filesize'] = filesize($file);
                  $file_list[$i]['dir_path'] = '';
                  $file_ext = strtolower(array_pop(explode('.', trim($file))));
                  $file_list[$i]['is_photo'] = in_array($file_ext, $this->ext_arr);
                  $file_list[$i]['filetype'] = $file_ext;
                }
                $file_list[$i]['filename'] = $filename; //文件名,包含扩展名
                $file_list[$i]['datetime'] = date('Y-m-d H:i:s', filemtime($file)); //文件最后修改时间
                $i++;
            }
            closedir($handle);
      }

      usort($file_list, array($this, 'cmp_func'));

      $result = array();
//相对于根目录的上一级目录
      $result['moveup_dir_path'] = $moveup_dir_path;
//相对于根目录的当前目录
      $result['current_dir_path'] = $current_dir_path;
//当前目录的URL
      $result['current_url'] = $current_url;
//文件数
      $result['total_count'] = count($file_list);
//文件列表数组
      $result['file_list'] = $file_list;

//输出JSON字符串
      header('Content-type: application/json; charset=UTF-8');
      //echo $this->save_path, $this->save_url;
      echo json_encode($result);
    }

    public function cmp_func($a, $b) {
      global $order;
      if ($a['is_dir'] && !$b['is_dir']) {
            return -1;
      } else if (!$a['is_dir'] && $b['is_dir']) {
            return 1;
      } else {
            if ($order == 'size') {
                if ($a['filesize'] > $b['filesize']) {
                  return 1;
                } else if ($a['filesize'] < $b['filesize']) {
                  return -1;
                } else {
                  return 0;
                }
            } else if ($order == 'type') {
                return strcmp($a['filetype'], $b['filetype']);
            } else {
                return strcmp($a['filename'], $b['filename']);
            }
      }
    }


}
2、创建控制器示例 editor.php
代码如下:

<?php
if (!defined('BASEPATH'))exit('No direct script access allowed');
class editor extends CI_Controller{
    public function __construct() {
      parent::__construct();
      $this->load->library('kindeditor');
    }

    public function index(){      
      $data['uploadJSON'] = base_url().'editor/upload'; //更改图片上传
      $data['manageJSON'] = base_url().'editor/manage';//更改图片浏览
      $this->load->view('editor',$data);
    }

    public function upload(){
      if(!empty ($_FILES)){
            $this->kindeditor->upload($_FILES);
      }
    }

    public function manage(){
      $path = isset($_GET['path']) ? $_GET['path'] : '';
      $this->kindeditor->manage($path);
    }


}





3、创建视图文件 editor.php

<html>
<head>
<title>Welcome to CodeIgniter</title>
<base href="<?=base_url();?>" />
<script charset="utf-8" type="text/javascript" src="assets/kindeditor/kindeditor.js"></script>
<script>
    KE.show({
      id : 'content1',
      allowFileManager : true,
      afterCreate : function(id) {
            KE.event.ctrl(document, 13, function() {
                KE.util.setData(id);
                document.forms['example'].submit();
            });
            KE.event.ctrl(KE.g.iframeDoc, 13, function() {
                KE.util.setData(id);
                document.forms['example'].submit();
            });// 让CI来处理KindEditor的上传和图片浏览
            KE.g.imageUploadJson = '<?=$uploadJSON;?>';
            KE.g.fileManagerJson = '<?=$manageJSON;?>';
      }
    });
</script>   
</head>
<body>
<h1>Welcome to CodeIgniter!</h1>
<h3>默认模式</h3>
<form name="example" method="post" action="<?=base_url().'admin/add';?>">
<textarea id="content1" name="content1" style="width:700px;height:300px;"></textarea>
<br />
<input type="submit" name="button" value="提交内容" />
(提交快捷键: Ctrl + Enter)
</form>
</body>




注:个人觉得还是有点儿麻烦,不过,kindeditor的路径,可以通过CI设定,还是方便了一点儿,希望大家有更好的办法。


幽蓝冰魄 发表于 2011-5-20 12:11:09

因为懒得调整kindeditor的样式,直接用了xheditor。

zero3412 发表于 2011-7-17 21:38:55

谢谢分享。

oodboo 发表于 2011-10-12 20:28:36

我用了一下,有问题啊。上传可以,就是管理显示不出来,能不能再解释一下。要注意哪些点。

oodboo 发表于 2011-10-23 21:18:51

在IE下测试发现上传图片后IE的进度条一直在进行中,就是完成不了。

coofee 发表于 2011-11-14 09:56:12

真是太好了!

zxw5052 发表于 2011-11-14 17:40:09

{:soso_e113:}顶一下

guo 发表于 2011-11-25 10:44:15

{:1_1:}

sdink 发表于 2011-11-25 11:21:51

kindeditor不错。支持国产   

小夏敏 发表于 2011-11-29 03:07:49

真的太好了··太感谢楼主了
页: [1] 2
查看完整版本: CI2.0中整合kindeditor