zfaka的layedit编辑器添加上传图片功能

2021.4.6亲测zfaka1.4.4版本

打开/public/res/mods/adminproducts.js文件,将第8到10行修改为

layedit.set({
    uploadImage: {
        url: '/'+ADMIN_DIR+'/products/imgupload',
        type: 'post'
    }
});
var edit_description=layedit.build('description',{
    tool: ['strong','italic','underline','|','del','left','center','right','link','unlink','face','image']
});     //建立编辑器

打开/application/modules/Admin/controllers/Products.php,在类里面添加函数

public function imguploadAction(){
    if ($this->AdminUser==FALSE AND empty($this->AdminUser)) {
        $data = array('code' => 1000, 'msg' => '请登录');
        Helper::response($data);
    }
    if(is_array($_FILES) AND !empty($_FILES) AND isset($_FILES['file'])){
        if(isset($_FILES["file"]["error"]) AND $_FILES["file"]["error"]){
            $data = array('code' => 1000, 'msg' =>$_FILES["file"]["error"]);
            Helper::response($data); 
        }else{
            try{
                $ext = pathinfo($_FILES['file']['name']);
                $ext = strtolower($ext['extension']);
                $tempFile = $_FILES['file']['tmp_name'];
                $targetPath  = UPLOAD_PATH.'/'.CUR_DATE;
                if( !is_dir($targetPath) ){
                    mkdir($targetPath,0777,true);
                }
                $filename=date("His");
                $new_file_name = $filename.'.'.$ext;
                $targetFile = $targetPath .'/'. $new_file_name;
                move_uploaded_file($tempFile,$targetFile);
                if( !file_exists( $targetFile ) ){
                    $data = array('code' => 1000, 'msg' => '上传失败');
                } elseif( !$imginfo=getimagesize($targetFile) ) {
                    $data = array('code' => 1000, 'msg' => '上传失败,文件不存在 ');
                } else {
                    \Yaf\Loader::import(FUNC_PATH.'/F_Img.php');
                    image_center_crop($targetFile, $imginfo[0], $imginfo[1], $targetFile);
                    $img = '/res/upload/'.CUR_DATE.'/'.$new_file_name;
                    $data = array('code' => 0, 'msg' => 'success','data'=>array('src'=>$img));
                }
            }catch(\Exception $e) {
                $data = array('code' => 1002, 'msg' => $e->getMessage(),'data'=>array());
            }
        }
    }else{
        $data = array('code' => 1000, 'msg' => '上传内容为空,请重新上传','data'=>array());
    }
    Helper::response($data);
}

 

原创文章,作者:365开发网,如若转载,请注明出处:https://www.365codes.com/772.html

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注