最的这一批需要高并发的小站迁往SAE,Kindeditor是一个好用的富文本编辑器
把附件file_manager_json.php,upload_json.php替换原kindeditor PHP目录下的同名文件
file_manager_json.php<?php <br />
<br>
// +----------------------------------------------------------------------<br>
// | Author: 左边 (加群:366504956(刚建,欢迎) 交流thinkphp下微信开发)<br>
// +----------------------------------------------------------------------<br>
<br>
require_once 'JSON.php';<br>
use sinacloud\sae\Storage as Storage;<br>
$s = new Storage();<br>
<br>
<br>
//Bucket<br>
$root_path = 'upload';<br>
<br>
//不存在就创建<br>
$pathArr = $s->listBuckets();<br>
<br>
if(!in_array($root_path,$pathArr) ){<br>
$s->putBucket($root_path,'.r:*');<br>
}<br>
<br>
<br>
//图片扩展名<br>
$ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');<br>
<br>
//目录名/必须时 image,flash等<br>
$dir_name = empty($_GET['dir']) ? '' : trim($_GET['dir']);<br>
<br>
if (!in_array($dir_name, array('', 'image', 'flash', 'media', 'file'))) {<br>
echo "Invalid Directory name.";<br>
exit;<br>
}<br>
<br>
<br>
//根据path参数,设置各路径和URL<br>
$moveup_dir_path = '';<br>
<br>
if (empty($_GET['path'])) {<br>
$current_path = $dir_name.'/';<br>
} else {<br>
///目录<br>
$path = $_GET['path'];<br>
$dirArr = array_filter( explode('/', $path) );<br>
$current_path = implode('/', $dirArr).'/';<br>
//上级目录<br>
<br>
$count = count($dirArr);<br>
if( $count > 1){<br>
//去掉最后一个<br>
unset( $dirArr[$count-1] );<br>
$moveup_dir_path = implode('/', $dirArr).'/';<br>
<br>
}<br>
<br>
<br>
}<br>
<br>
//排序形式,name or size or type<br>
$order = empty($_GET['order']) ? 'name' : strtolower($_GET['order']);<br>
<br>
//不允许使用..移动到上一级目录<br>
if (preg_match('/\.\./', $current_path)) {<br>
echo 'Access is not allowed.';<br>
exit;<br>
}<br>
//最后一个字符不是/<br>
if (!preg_match('/\/$/', $current_path)) {<br>
echo 'Parameter is not valid.';<br>
exit;<br>
}<br>
<br>
<br>
//遍历目录取得文件信息<br>
$file_list = array();<br>
<br>
$f = $s->getBucket($root_path,$current_path);<br>
//Url<br>
$current_url = $s->getUrl($root_path,$current_path);<br>
<br>
$pack = array();<br>
if($f && is_array($f) ){<br>
<br>
foreach ($f as $v) {<br>
$file = $v;<br>
//$filename = $file['name'];<br>
//去掉文件夹<br>
$filename = str_replace($current_path,'',$file['name']);<br>
$pathinfo = pathinfo($filename);<br>
if ($pathinfo['dirname'] == '.' ) {<br>
//这是文件<br>
$fileArr['is_dir'] = false;<br>
$fileArr['has_file'] = false;<br>
$fileArr['filesize'] = $file['bytes'];<br>
$fileArr['dir_path'] = '';<br>
$file_ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));<br>
$fileArr['is_photo'] = in_array($file_ext, $ext_arr);<br>
$fileArr['filetype'] = $file_ext;<br>
$fileArr['filename'] = $filename; //文件名,包含扩展名<br>
$fileArr['datetime'] = date('Y-m-d H:i:s', strtotime($file['last_modified']) );<br>
$file_list[] = $fileArr; <br>
//文件最后修改时间<br>
}else{<br>
//这是文件夹;去掉重复的<br>
if(!in_array($pathinfo['dirname'], $pack)){<br>
$pack[] = $pathinfo['dirname'];<br>
$bag['is_dir'] = true; //是否文件夹<br>
$bag['has_file'] = true; //文件夹是否包含文件<br>
$bag['filesize'] = 0; //文件大小<br>
$bag['is_photo'] = false; //是否图片<br>
$bag['filetype'] = ''; //文件类别,用扩展名判断<br>
$bag['filename'] = $pathinfo['dirname']; //文件夹名<br>
$bag['datetime'] = date('Y-m-d H:i:s', strtotime($file['last_modified']) );<br>
<br>
$file_list[] = $bag; <br>
<br>
<br>
}<br>
}<br>
<br>
}<br>
<br>
<br>
}<br>
<br>
<br>
//排序<br>
function cmp_func($a, $b) {<br>
global $order;<br>
if ($a['is_dir'] && !$b['is_dir']) {<br>
return -1;<br>
} else if (!$a['is_dir'] && $b['is_dir']) {<br>
return 1;<br>
} else {<br>
if ($order == 'size') {<br>
if ($a['filesize'] > $b['filesize']) {<br>
return 1;<br>
} else if ($a['filesize']
return -1;<br>
} else {<br>
return 0;<br>
}<br>
} else if ($order == 'type') {<br>
return strcmp($a['filetype'], $b['filetype']);<br>
} else {<br>
return strcmp($a['filename'], $b['filename']);<br>
}<br>
}<br>
}<br>
usort($file_list, 'cmp_func');<br>
<br>
$result = array();<br>
//相对于根目录的上一级目录<br>
$result['moveup_dir_path'] = $moveup_dir_path;<br>
//相对于根目录的当前目录<br>
$result['current_dir_path'] = $current_path;<br>
//当前目录的URL<br>
$result['current_url'] = $current_url;<br>
//文件数<br>
$result['total_count'] = count($file_list);<br>
//文件列表数组<br>
$result['file_list'] = $file_list;<br>
<br>
//输出JSON字符串<br>
header('Content-type: application/json; charset=UTF-8');<br>
$json = new Services_JSON();<br>
echo $json->encode($result);
upload_json.php<?php <br />
// +----------------------------------------------------------------------<br>
// | Author: 左边 (加群:366504956(刚建,欢迎) 交流thinkphp下微信开发)<br>
// +----------------------------------------------------------------------<br>
<br>
require_once 'JSON.php';<br>
use sinacloud\sae\Storage as Storage;<br>
$s = new Storage();<br>
<br>
<br>
//Bucket<br>
$root_path = 'upload';<br>
<br>
//不存在就创建<br>
$pathArr = $s->listBuckets();<br>
<br>
if(!in_array($root_path,$pathArr) ){<br>
$s->putBucket($root_path,'.r:*');<br>
}<br>
<br>
//定义允许上传的文件扩展名<br>
$ext_arr = array(<br>
'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),<br>
'flash' => array('swf', 'flv'),<br>
'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),<br>
'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),<br>
);<br>
//最大文件大小<br>
$max_size = 2048000;<br>
<br>
<br>
<br>
//PHP上传失败<br>
if (!empty($_FILES['imgFile']['error'])) {<br>
switch($_FILES['imgFile']['error']){<br>
case '1':<br>
$error = '超过php.ini允许的大小。';<br>
break;<br>
case '2':<br>
$error = '超过表单允许的大小。';<br>
break;<br>
case '3':<br>
$error = '图片只有部分被上传。';<br>
break;<br>
case '4':<br>
$error = '请选择图片。';<br>
break;<br>
case '6':<br>
$error = '找不到临时目录。';<br>
break;<br>
case '7':<br>
$error = '写文件到硬盘出错。';<br>
break;<br>
case '8':<br>
$error = 'File upload stopped by extension。';<br>
break;<br>
case '999':<br>
default:<br>
$error = '未知错误。';<br>
}<br>
alert($error);<br>
}<br>
<br>
//有上传文件时<br>
if (empty($_FILES) === false) {<br>
//原文件名<br>
$file_name = $_FILES['imgFile']['name'];<br>
//服务器上临时文件名<br>
$tmp_name = $_FILES['imgFile']['tmp_name'];<br>
//文件大小<br>
$file_size = $_FILES['imgFile']['size'];<br>
//检查文件名<br>
if (!$file_name) {<br>
alert("请选择文件。");<br>
}<br>
<br>
//检查是否已上传<br>
if (@is_uploaded_file($tmp_name) === false) {<br>
alert("上传失败。");<br>
}<br>
//检查文件大小<br>
if ($file_size > $max_size) {<br>
alert("上传文件大小超过限制。");<br>
}<br>
//检查目录名<br>
$dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);<br>
if (empty($ext_arr[$dir_name])) {<br>
alert("目录名不正确。");<br>
}<br>
//获得文件扩展名<br>
$temp_arr = explode(".", $file_name);<br>
$file_ext = array_pop($temp_arr);<br>
$file_ext = trim($file_ext);<br>
$file_ext = strtolower($file_ext);<br>
//检查扩展名<br>
if (in_array($file_ext, $ext_arr[$dir_name]) === false) {<br>
alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。");<br>
}<br>
//创建文件夹<br>
$save_path .= $dir_name . "/";<br>
$ymd = date("Ymd");<br>
$save_path .= $ymd . "/";<br>
//新文件名<br>
$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;<br>
//移动文件<br>
$file_path = $save_path . $new_file_name;<br>
<br>
<br>
$r = $s->putObjectFile( $tmp_name,$root_path, $file_path);<br>
<br>
if (!$r) {<br>
alert("上传文件失败。");<br>
}<br>
<br>
$file_url = $s->getUrl( $root_path,$file_path); <br>
<br>
header('Content-type: text/html; charset=UTF-8');<br>
$json = new Services_JSON();<br>
echo $json->encode(array('error' => 0, 'url' => $file_url));<br>
exit;<br>
}<br>
<br>
function alert($msg) {<br>
header('Content-type: text/html; charset=UTF-8');<br>
$json = new Services_JSON();<br>
echo $json->encode(array('error' => 1, 'message' => $msg));<br>
exit;<br>
}
kindeidtor.json.php.zip ( 3.43 KB 下载:4 次 )
AD:真正免费,域名+虚机+企业邮箱=0元