作者:白狼出處: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教程有興趣的朋友有所幫助。