Heim > Artikel > Backend-Entwicklung > yii2解决百度编辑器umeditor图片上传问题 ewebeditor编辑器下载 metaeditor编辑器下载 ewebeditor编辑器升
作者:白狼 出处:http://www.manks.top/article/yii2_umeditor_upload本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
yii2框架整合了百度编辑器,因为文件上传采用的是yii2自带的UploadedFile,这就难免umeditor上传不成功问题,解决问题的只需要两个操作步骤,我们来看看具体实现
首先我们先把umeditor的配置搞好,这里只需要更改imageUrl配置项即可,我们修改其指向/tools/um-upload
那下一步自然是实现/tools/um-upload方法了,
按照ueditor的实现来看,这里我们上传成功后只需要返回成功信息即可
<span>use</span><span> backend\models\Upload; </span><span>use</span><span> yii\web\UploadedFile; </span><span>/*</span><span>* * 百度umeditor上传 </span><span>*/</span><span>public</span><span>function</span><span> actionUmUpload () { </span><span>$model</span> = <span>new</span><span> Upload(); </span><span>if</span> (Yii::<span>$app</span>->request-><span>isPost) { </span><span>$model</span>-><span>file</span> = UploadedFile::getInstance(<span>$model</span>, &<span>#</span><span>39;file');</span><span>$dir</span> =<span> ‘文件保存目录’; </span><span>if</span> (!<span>is_dir</span>(<span>$dir</span><span>)) </span><span>mkdir</span>(<span>$dir</span><span>); </span><span>if</span> (<span>$model</span>-><span>validate()) { </span><span>$fileName</span> = <span>$model</span>-><span>file</span>-><span>baseName</span> . "<span>." . $model->file->extension;</span><span>$dir</span> = <span>$dir</span>."<span>/". $fileName;</span><span>$model</span>-><span>file</span>->saveAs(<span>$dir</span><span>); </span><span>$info</span> =<span> [ </span>"originalName" => <span>$model</span>-><span>file</span>-><span>baseName</span>, "name" => <span>$model</span>-><span>file</span>-><span>baseName</span>, "url" => <span>$dir</span>, "size" => <span>$model</span>-><span>file</span>->size, "type" => <span>$model</span>-><span>file</span>->type, "state" => "<span>SUCCESS",</span><span> ]; </span><span>exit</span>(json_encode(<span>$info</span><span>)); } } }</span>
特别提醒:上述返回的$info信息中state状态只能是SUCCESS,区分大小写
关于Yii图片上传请参考Yii2文件上传
关于yii整合百度编辑器请参考yii2整合百度编辑器umeditor
以上就介绍了yii2解决百度编辑器umeditor图片上传问题,包括了editor,百度编辑器方面的内容,希望对PHP教程有兴趣的朋友有所帮助。