在PHP项目中集成UEditor富文本编辑器是后台内容管理的常见需求,下面将从部署到功能实现逐步讲解完整流程。

一、UEditor文件部署
首先从UEditor官方仓库下载对应版本的PHP版压缩包,解压后得到ueditor文件夹,将其放到项目根目录的public/static目录下,目录结构如下:
- public/static/ueditor/
- ├── ueditor.config.js # 编辑器配置文件
- ├── ueditor.all.js # 核心JS文件
- ├── lang/ # 语言包目录
- ├── php/ # PHP后端处理目录
- └── themes/ # 主题样式目录
二、前端页面引入与初始化
在需要使用编辑器的PHP页面中,先引入UEditor的核心JS文件,再创建编辑器容器并初始化。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>UEditor集成示例</title>
<!-- 引入UEditor配置文件 -->
<script type="text/javascript" src="/static/ueditor/ueditor.config.js"></script>
<!-- 引入UEditor核心文件 -->
<script type="text/javascript" src="/static/ueditor/ueditor.all.js"></script>
<!-- 引入语言包 -->
<script type="text/javascript" src="/static/ueditor/lang/zh-cn/zh-cn.js"></script>
</head>
<body>
<!-- 编辑器容器 -->
<script id="editor" type="text/plain" style="width:100%;height:400px;">
这里可以写初始内容
</script>
<script type="text/javascript">
// 初始化UEditor实例
var ue = UE.getEditor('editor', {
// 自定义配置项,会覆盖ueditor.config.js中的默认配置
initialFrameWidth: '100%',
initialFrameHeight: 400,
serverUrl: '/static/ueditor/php/controller.php' // 后端处理接口地址
});
</script>
</body>
</html>三、PHP后端配置修改
UEditor的上传等功能依赖后端接口处理,需要修改ueditor/php/config.json文件适配项目需求,核心配置项说明如下:
| 配置项 | 说明 | 示例值 |
|---|---|---|
| imagePathFormat | 图片上传后保存路径格式 | /uploads/images/{yyyy}{mm}{dd}/{time}{rand:6} |
| imageUrlPrefix | 图片访问URL前缀 | "" |
| videoPathFormat | 视频上传后保存路径格式 | /uploads/videos/{yyyy}{mm}{dd}/{time}{rand:6} |
| filePathFormat | 附件上传后保存路径格式 | /uploads/files/{yyyy}{mm}{dd}/{time}{rand:6} |
需要确保PHP环境中开启了file_uploads、gd扩展,并且上传目录有写入权限,否则会出现上传失败的问题。
四、获取编辑器内容
在前端提交表单时,可以通过UEditor提供的API获取编辑器中的内容,示例如下:
// 获取编辑器纯文本内容
var plainText = ue.getContentTxt();
// 获取带HTML标签的完整内容
var htmlContent = ue.getContent();
// 设置编辑器内容
ue.setContent('新的编辑器内容');将获取到的htmlContent通过表单提交到PHP后端,就可以保存到数据库中使用。
五、常见问题解决
- 如果上传图片提示后端配置项没有正常加载,检查
serverUrl是否正确指向controller.php,且文件存在。 - 如果上传后图片无法访问,检查
imagePathFormat配置的路径是否存在,以及是否有访问权限。 - 如果需要自定义上传逻辑,可以修改
ueditor/php/controller.php中的处理逻辑,适配项目的上传规则。
UEditorphp_integrationrich_text_editorueditor_config修改时间:2026-05-28 15:50:31