thinkphp整合kindeditor,修改上传方式为thinkphp自带
批量图片上传不了?这是由于swfupload使用了Flash,丢失了session造成的,好多解决办法,请自行百度。
1.调用编辑器:
<LINK href="__EDITOR__/themes/default/default.css" type="text/css" rel="stylesheet">
<script charset="utf-8" src="__EDITOR__/kindeditor-min.js" type="text/javascript"></script>
<script charset="utf-8" src="__EDITOR__/lang/zh_CN.js" type="text/javascript"></script>
这里因为我是采用普通分组,kindeditor编辑器我是放在根目录Public下,故在后台配置文件里,新增加了一荐配置'__EDITOR__'=>.'/Public/editor',只是为了写起来方便,也可以在上面直接把路径写起来;
在公共配置文件里,新增加配置uploadpath => 'Data/upload';我是放在根目录Data/upload下面,方便日后修改。
2.配置上传方式:
uploadJson : '{:U("Editor/upload",'','')}',
fileManagerJson : '{:U("Editor/file_manage",'','')}',
3.下面就把Editor类发出来,仅供参考:
<?php
class EditorAction extends AdmincheckAction{
//编辑器文件上传
public function upload(){
//图片上传路径
$php_path = './';
$php_url = . '/';
$dir_name = I("get.dir",'image','htmlspecialchars,trim');
$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'),
);
if (empty ( $ext_arr[$dir_name] )) {
$this->ajaxReturn (array('error' => 1, 'message' => '未知错误'));
exit ();
}
$php_path .= C('uploadpath') . $dir_name . "/";
$php_url .= C('uploadpath') . $dir_name . "/";
import('ORG.NET.UploadFile');
$upload = new UploadFile();
$upload->autoSub = true; //自动建子目录
$upload->subType = 'date'; //按日期创建文件夹
$upload->dateFormat = 'Ymd'; //按年月命名
$upload->allowExts = $ext_arr[$dir_name]; //允许上传类型
$upload->saveRule = 'uniqid';
$upload->savePath = $php_path;
$upload->upload();
//取得最后一次错误信息
$uploaderror = $upload->getErrorMsg();
//取成功返回信息
$jieguo = $upload->getUploadFileInfo();
//错误信息空和有返回成功信息则返回URL
if($uploaderror=='' && $jieguo){
$this->ajaxReturn (array('error' => 0, 'url' => $php_url.$jieguo[0]['savename']));
exit ();
}else{
$this->ajaxReturn (array('error' => 1, 'message' => $uploaderror));
exit ();
}
}
//编辑器文件浏览器
public function file_manage(){
$uploadpath = C('uploadpath');
$php_path = dirname(__FILE__) . '/../../../../';
$php_url = . '/';
$root_path = $php_path . $uploadpath;
$root_url = $php_url . $uploadpath;
$ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
//目录名
$dir_name = empty($_GET['dir']) ? '' : trim($_GET['dir']);
if (!in_array($dir_name, array('', 'image', 'flash', 'media', 'file'))) {
echo "无效的目录名";
exit;
}
if ($dir_name !== '') {
$root_path .= $dir_name . "/";
$root_url .= $dir_name . "/";
if (!file_exists($root_path)) {
mkdir($root_path);
}
}
//根据path参数,设置各路径和URL
if (empty($_GET['path'])) {
$current_path = realpath($root_path) . '/';
$current_url = $root_url;
$current_dir_path = '';
$moveup_dir_path = '';
} else {
$current_path = realpath($root_path) . '/' . $_GET['path'];
$current_url = $root_url . $_GET['path'];
$current_dir_path = $_GET['path'];
$moveup_dir_path = preg_replace('/(.*?)[^\/]+\/$/', '$1', $current_dir_path);
}
echo realpath($root_path);
//排序形式,name or size or type
$order = empty($_GET['order']) ? 'name' : strtolower($_GET['order']);
//不允许使用..移动到上一级目录
if (preg_match('/\.\./', $current_path)) {
echo '没有权限访问';
exit;
}
//最后一个字符不是/
if (!preg_match('/\/$/', $current_path)) {
echo '无效的参数';
exit;
}
//目录不存在或不是目录
if (!file_exists($current_path) || !is_dir($current_path)) {
echo '文件夹不存在';
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(pathinfo($file, PATHINFO_EXTENSION));
$file_list[$i]['is_photo'] = in_array($file_ext, $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);
}
//排序
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']);
}
}
}
usort($file_list, '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;
$this->ajaxReturn ( $result );
}
}
?>