tp5环境使用ueditor时,一般把它放在public目录下的static目录下
打开ueditor/php/config.json文件,需要重新设置图片保存的路径。上传的图片一般放在tp5的public目录下的uploads目录中
"imagePathFormat": "/tp5/public/uploads/image/{yyyy}{mm}{dd}/{time}{rand:6}",
"scrawlPathFormat": "/tp5/public/uploads/image/{yyyy}{mm}{dd}/{time}{rand:6}"
"snapscreenPathFormat": "/tp5/public/uploads/image/{yyyy}{mm}{dd}/{time}{rand:6}"
"catcherPathFormat": "/tp5/public/uploads/image/{yyyy}{mm}{dd}/{time}{rand:6}"
图片存放路径是相对于web根目录的,比如:
/tp5/public/uploads/image表示图片存放在web根目录下的tp5/public/uploads/image文件夹下
下面我们实现一个简单的案例:发布一条新闻,保存到数据库中
数据表结构
页面参考代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="__STATIC__/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="__STATIC__/ueditor/ueditor.all.min.js"></script>
<script type="text/javascript" src="__STATIC__/ueditor/lang/zh-cn/zh-cn.js"></script>
</head>
<body>
<form action="{:url('addarticle')}" method="post">
<div>
<label>标题</label>
<input type="text" name="title">
</div>
<div>
<label>正文</label>
<script id="editor" name="content" type="text/plain" style="width:1024px;height:500px;"></script>
</div>
<div>
<input type="submit" value="保存">
</div>
</form>
<script>
var ue = UE.getEditor('editor', {
autoHeightEnabled: true,
autoFloatEnabled: true
});
</script>
</body>
</html>
控制器参考代码
<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Exception;
class Article extends Controller
{
public function index()
{
return $this->fetch();
}
public function addarticle()
{
$title = input('post.title');
$content = input('post.content');
$data = ['title'=>$title,'content'=>$content];
try{
Db::name('article')->insert($data);
echo '添加成功';
}catch (Exception $ex){
$this->error('添加失败,' . $ex->getMessage());
}
}
}