ITeye博客

为 KindEditor 实现图片上传

libudi · 2009-09-26 09:34 · Lysee · Web前端 · Markdown

用 Lysee 开发了一个 BBS,发贴和回复用的是 KindEditor ,图片上传这块用 Lysee 照抄了 upload.php,经测试没有问题,发上来大家看看。

 

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">';
<title>{:bbsys::SITE_NAME:} - Insert Image</title>
</head>
<body>  
<!--{:
string image_save_path(string file_sum)
{
  int hv = file_sum.hash();
  string rv = format("%.3d", [(hv % 256) + 1]);
  string sv = format("%.3d", [(hv \ 256) % 256 + 1]);
  string path = incPD(web_upload_path + rv) + sv;
  sys::mkdir(path);
  return incPD(path);
}

string img_URL = "";  
string tmp_file = ${request.imgFile->file};
string ext = ${request.imgFile}.fileExt().lower();
if (ext in [".jpg", ".jpeg", ".png", ".gif"]) { 
  string sum = tmp_file.md5sum(true);
  string path = image_save_path(sum);
  string img_file = path + sum + ext;
  if (isfile(img_file) or sys::cp(tmp_file, img_file))
    img_URL = veryUD(img_file.copy(length(web_path), length(img_file)));
}

= """<script language="javascript">"""; 
= (img_URL) ? @"""parent.KE.plugin["image"].insert("%{request.id}", 
                  "%(img_URL)", "%{request.imgTitle}", "%{request.imgWidth}",
                  "%{request.imgHeight}", "%{request.imgBorder}");"""
              :"""alert("Failed inserting image!");history.back();""";
= """</script>""";
:}-->
</body>
</html>

评论(1)

  1. #1 楼 · libudi · 2009-09-26 09:46

    JavaEye 的编辑器还有问题,重新编辑 BLOG 内容时如果原文最后是代码,新打开的编辑器光标无法移动到代码的后边 8)

    补充显示 upload.php 作作对照。

    
    <?php
    
    //文件保存目录路径
    $save_path = './../attached/';
    //文件保存目录URL
    $save_url = './../attached/';
    //定义允许上传的文件扩展名
    $ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
    //最大文件大小
    $max_size = 1000000;
    //更改目录权限
    @mkdir($save_path, 0777);
    
    //有上传文件时
    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_dir($save_path) === false) {
            alert("上传目录不存在。");
        }
        //检查目录写权限
        if (@is_writable($save_path) === false) {
            alert("上传目录没有写权限。");
        }
        //检查是否已上传
        if (@is_uploaded_file($tmp_name) === false) {
            alert("临时文件可能不是上传文件。");
        }
        //检查文件大小
        if ($file_size > $max_size) {
            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) === false) {
            alert("上传文件扩展名是不允许的扩展名。");
        }
        //移动文件
        $file_path = $save_path . $file_name;
        if (move_uploaded_file($tmp_name, $file_path) === false) {
            alert("上传文件失败。");
        }
        $file_url = $save_url . $file_name;
        //插入图片,关闭层
        echo '<html>';
        echo '<head>';
        echo '<title>Insert Image</title>';
        echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';
        echo '</head>';
        echo '<body>';
        echo '<script type="text/javascript">parent.KE.plugin["image"].insert("' . $_POST['id'] . '", "' . $file_url . '","' . $_POST['imgtitle'] . '","' . $_POST['imgwidth'] . '","' . $_POST['imgheight'] . '","' . $_POST['imgborder'] . '");</script>';
        echo '</body>';
        echo '</html>';
    }
    
    //提示,关闭层
    function alert($msg)
    {
        echo '<html>';
        echo '<head>';
        echo '<title>error</title>';
        echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';
        echo '</head>';
        echo '<body>';
        echo '<script type="text/javascript">alert("'.$msg.'");history.back();</script>';
        echo '</body>';
        echo '</html>';
        exit;
    }
    ?>
    
    

评论功能维护中,当前仅展示历史评论。