您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

支持SAE的Kindeditor上传和管理部分代码

2024/6/29 6:04:21发布15次查看
最的这一批需要高并发的小站迁往sae,kindeditor是一个好用的富文本编辑器
把附件file_manager_json.php,upload_json.php替换原kindeditor php目录下的同名文件
file_manager_json.phplistbuckets();
if(!in_array($root_path,$patharr) ){
    $s->putbucket($root_path,'.r:*');
}
//图片扩展名
$ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
//目录名/必须时 image,flash等
$dir_name = empty($_get['dir']) ? '' : trim($_get['dir']);
if (!in_array($dir_name, array('', 'image', 'flash', 'media', 'file'))) {
    echo invalid directory name.;
    exit;
}
//根据path参数,设置各路径和url
$moveup_dir_path = '';
if (empty($_get['path'])) {
    $current_path = $dir_name.'/';
} else {
    ///目录
    $path = $_get['path'];
    $dirarr = array_filter( explode('/', $path) );
    $current_path = implode('/', $dirarr).'/';
    //上级目录
$count = count($dirarr);
    if( $count > 1){
        //去掉最后一个
        unset( $dirarr[$count-1] );
        $moveup_dir_path = implode('/', $dirarr).'/';
}
}
//排序形式,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;
}
//遍历目录取得文件信息
$file_list = array();
$f = $s->getbucket($root_path,$current_path);
//url
$current_url = $s->geturl($root_path,$current_path);
$pack = array();
if($f && is_array($f) ){
foreach ($f as $v) {
        $file = $v;
        //$filename = $file['name'];
        //去掉文件夹
        $filename = str_replace($current_path,'',$file['name']);
        $pathinfo = pathinfo($filename);
        if ($pathinfo['dirname'] == '.' ) {
            //这是文件
            $filearr['is_dir'] = false;
            $filearr['has_file'] = false;
            $filearr['filesize'] = $file['bytes'];
            $filearr['dir_path'] = '';
            $file_ext = strtolower(pathinfo($filename, pathinfo_extension));
            $filearr['is_photo'] = in_array($file_ext, $ext_arr);
            $filearr['filetype'] = $file_ext;
            $filearr['filename'] = $filename; //文件名,包含扩展名
            $filearr['datetime'] = date('y-m-d h:i:s', strtotime($file['last_modified']) );
            $file_list[] = $filearr; 
        //文件最后修改时间
        }else{
            //这是文件夹;去掉重复的
            if(!in_array($pathinfo['dirname'], $pack)){
                $pack[] = $pathinfo['dirname'];
                $bag['is_dir'] = true; //是否文件夹
                $bag['has_file'] = true; //文件夹是否包含文件
                $bag['filesize'] = 0; //文件大小
                $bag['is_photo'] = false; //是否图片
                $bag['filetype'] = ''; //文件类别,用扩展名判断
                $bag['filename'] = $pathinfo['dirname']; //文件夹名
                $bag['datetime'] = date('y-m-d h:i:s', strtotime($file['last_modified']) );
$file_list[] = $bag;
}
        }
}
}
//排序
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']                 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_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');
$json = new services_json();
echo $json->encode($result);upload_json.phplistbuckets();
if(!in_array($root_path,$patharr) ){
    $s->putbucket($root_path,'.r:*');
}
//定义允许上传的文件扩展名
$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'),
    );
//最大文件大小
$max_size = 2048000;
//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 = '未知错误。';
    }
    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) {
        alert(请选择文件。);
    }
//检查是否已上传
    if (@is_uploaded_file($tmp_name) === false) {
        alert(上传失败。);
    }
    //检查文件大小
    if ($file_size > $max_size) {
        alert(上传文件大小超过限制。);
    }
    //检查目录名
    $dir_name = empty($_get['dir']) ? 'image' : trim($_get['dir']);
    if (empty($ext_arr[$dir_name])) {
        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, $ext_arr[$dir_name]) === false) {
        alert(上传文件扩展名是不允许的扩展名。\n只允许 . implode(,, $ext_arr[$dir_name]) . 格式。);
    }
    //创建文件夹
    $save_path .= $dir_name . /;
    $ymd = date(ymd);
    $save_path .= $ymd . /;
    //新文件名
    $new_file_name = date(ymdhis) . '_' . rand(10000, 99999) . '.' . $file_ext;
    //移动文件
    $file_path = $save_path . $new_file_name;
$r = $s->putobjectfile( $tmp_name,$root_path,  $file_path);
if (!$r) {
        alert(上传文件失败。);
    }
$file_url =  $s->geturl( $root_path,$file_path);
header('content-type: text/html; charset=utf-8');
    $json = new services_json();
    echo $json->encode(array('error' => 0, 'url' => $file_url));
    exit;
}
function alert($msg) {
    header('content-type: text/html; charset=utf-8');
    $json = new services_json();
    echo $json->encode(array('error' => 1, 'message' => $msg));
    exit;
}
kindeidtor.json.php.zip ( 3.43 kb 下载:4 次 )
ad:真正免费,域名+虚机+企业邮箱=0元
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product