╰↓①秒つ. 发表于 2011-11-29 13:35:43

这样子整个editor感觉木有用js整合来的简单。不过也是一个方法,顶下吧

メ開始べ愛♀ 发表于 2012-2-1 14:31:00

楼主 kindeditor 共享下吧 还有kindeditor要放在哪个地方

メ開始べ愛♀ 发表于 2012-2-1 14:32:47

js 我也整合了 但是 就是图片上传不了

tinsn 发表于 2012-6-12 23:12:34

我修改了下,补了些内容


class Kindeditor {
    //文件保存目录路径
    private $save_path;
    //文件保存目录URL
    private $save_url;
    //定义允许上传的文件扩展名
    private $ext_arr;
    //最大文件大小
    private $max_size;

    public function setControlParams($save_path,$save_url,$ext_arr,$max_size ) {
      //文件保存目录路径
      if ( empty($save_path)) {
            $save_path ='assets/kindeditor/attached/';
      }
      $this->save_path = realpath($save_path) . '/';
      //文件保存目录URL
      if (empty($save_url)){
            $save_url = base_url().$save_path;
      }
      $this->save_url =   $save_url;
      //定义允许上传的文件扩展名
       if (empty($ext_arr)) {
         $ext_arr = array(
                'image' =>array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
                'flash' => array('swf', 'flv'),
                'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
                'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
            );
      }
      $this->ext_arr = $ext_arr;

      //最大文件大小
      if (empty($max_size)){
            $max_size = 1000000;
      }
      $this->max_size = $max_size;
    }

    public function upload() {
      //PHP上传失败
      if (!empty($_FILES['imgFile']['error'])) {
            switch($_FILES['imgFile']['error']){
                case '1':
                  $error = '超过php.ini允许的大小。';
                  break;
                case '2':
                  $error = '超过表单允许的大小。';
                  break;
                case '3':
                  $error = '图片只有部分被上传。';
                  break;
                case '4':
                  $error = '请选择图片。';
                  break;
                case '6':
                  $error = '找不到临时目录。';
                  break;
                case '7':
                  $error = '写文件到硬盘出错。';
                  break;
                case '8':
                  $error = 'File upload stopped by extension。';
                  break;
                case '999':
                default:
                  $error = '未知错误。';
            }
            $this->alert($error);
      }
      //有上传文件时
      if (empty($_FILES) === false) {
            //原文件名
            $file_name = $_FILES['imgFile']['name'];
            //服务器上临时文件名
            $tmp_name = $_FILES['imgFile']['tmp_name'];
            //文件大小
            $file_size = $_FILES['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("上传文件大小超过限制。");
            }
            //检查目录名
            $dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
            if (empty($this->ext_arr[$dir_name])) {
                $this->alert("目录名不正确。".$_GET['dir']);
            }
            //获得文件扩展名
            $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[$dir_name]) === false) {
                $this->alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $this->ext_arr[$dir_name]) . "格式。");
            }
            //创建文件夹
            if ($dir_name !== '') {
                $this->save_path .= $dir_name . "/";
                $this->save_url .= $dir_name . "/";
                if (!file_exists($this->save_path)) {
                  mkdir($this->save_path);
                }
            }
            $ymd = date("Ymd");
            $this->save_path .= $ymd . "/";
            $this->save_url .= $ymd . "/";
            if (!file_exists($this->save_path)) {
                mkdir($this->save_path);
            }
            //新文件名
            $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;
    }
}

0day_You 发表于 2013-9-18 15:39:33

其实我直接简单的做成一个view,然后任意位置调用:
//初始化内容编辑器            
$kineditor = array(
                        'name' => 'text',
                        'id' => "content_text",
                        'value' => $news['text'],
                        'style' => ""      //附加的样式,重置模板默认样式
                        //other ...
                );
                $this->load->view('plugin/kindeditor/kindeditor',$kineditor);
刚刚接触时想到的方案,我觉得这样反而更灵活
我有个问题是我的图片浏览器怎么只有加在状态不出来内容...


纯屌丝 发表于 2013-10-28 21:41:26

tinsn 发表于 2012-6-12 23:12 static/image/common/back.gif
我修改了下,补了些内容

嗯,而且你还很不厚道的删了manage,而且你新增的函数命名还违反ci命名规范。

纯屌丝 发表于 2013-10-28 21:49:55

本帖最后由 纯屌丝 于 2013-10-28 21:52 编辑

屌丝把改后的贴上来吧:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Kindeditor
{
    # 文件保存目录路径
    private $save_path;
    # 文件保存目录URL
    private $save_url;
    # 定义允许上传的文件扩展名
    private $ext_arr;
    # 最大文件大小
    private $max_size;

    public function config($save_path, $save_url, $ext_arr, $max_size)
    {
      # 文件保存目录路径
      if (empty($save_path)) {
            $save_path = 'upload/';
      }
      $this->save_path = realpath() . $save_path . '/';
      # 文件保存目录URL
      if (empty($save_url)) {
            $save_url = base_url() . $save_path;
      }
      $this->save_url = $save_url;
      # 定义允许上传的文件扩展名
      if (empty($ext_arr)) {
            $ext_arr = array(
                'image' => array(
                  'gif',
                  'jpg',
                  'jpeg',
                  'png',
                  'bmp'
                ),
                'flash' => array(
                  'swf',
                  'flv'
                ),
                'media' => array(
                  'swf',
                  'flv',
                  'mp3',
                  'wav',
                  'wma',
                  'wmv',
                  'mid',
                  'avi',
                  'mpg',
                  'asf',
                  'rm',
                  'rmvb'
                ),
                'file' => array(
                  'doc',
                  'docx',
                  'xls',
                  'xlsx',
                  'ppt',
                  'htm',
                  'html',
                  'txt',
                  'zip',
                  'rar',
                  'gz',
                  'bz2'
                )
            );
      }
      $this->ext_arr = $ext_arr;

      # 最大文件大小
      if (empty($max_size)) {
            $max_size = 1000000;
      }
      $this->max_size = $max_size;
    }

    public function upload()
    {
      # PHP上传失败
      if (!empty($_FILES['imgFile']['error'])) {
            switch ($_FILES['imgFile']['error']) {
                case '1':
                  $error = '超过php.ini允许的大小。';
                  break;
                case '2':
                  $error = '超过表单允许的大小。';
                  break;
                case '3':
                  $error = '图片只有部分被上传。';
                  break;
                case '4':
                  $error = '请选择图片。';
                  break;
                case '6':
                  $error = '找不到临时目录。';
                  break;
                case '7':
                  $error = '写文件到硬盘出错。';
                  break;
                case '8':
                  $error = 'File upload stopped by extension。';
                  break;
                case '999':
                default:
                  $error = '未知错误。';
            }
            $this->alert($error);
      }
      # 有上传文件时
      if (empty($_FILES) === false) {
            # 原文件名
            $file_name = $_FILES['imgFile']['name'];
            # 服务器上临时文件名
            $tmp_name= $_FILES['imgFile']['tmp_name'];
            # 文件大小
            $file_size = $_FILES['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("上传文件大小超过限制。");
            }
            # 检查目录名
            $dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
            if (empty($this->ext_arr[$dir_name])) {
                $this->alert("目录名不正确。" . $_GET['dir']);
            }
            # 获得文件扩展名
            $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[$dir_name]) === false) {
                $this->alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $this->ext_arr[$dir_name]) . "格式。");
            }
            # 创建文件夹
            if ($dir_name !== '') {
                $this->save_path .= $dir_name . "/";
                $this->save_url .= $dir_name . "/";
                if (!file_exists($this->save_path)) {
                  mkdir($this->save_path);
                }
            }
            $ymd = date("Ymd");
            $this->save_path .= $ymd . "/";
            $this->save_url .= $ymd . "/";
            if (!file_exists($this->save_path)) {
                mkdir($this->save_path);
            }
            # 新文件名
            $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']);
            }
      }
    }
}


# 调用时使用下面代码,config里写上相较于apppath的相对路径,结尾的“/”不能丢
$this->load->library('kindeditor');
$this->kindeditor->config('upload/article/');





Hick 发表于 2013-10-29 22:15:40

对技术族来讲,其实用 markdown 这样的纯文本挺好
页: 1 [2]
查看完整版本: CI2.0中整合kindeditor