Home php教程 php手册 yii2解决百度编辑器umeditor图片上传问题 - 白狼栈

yii2解决百度编辑器umeditor图片上传问题 - 白狼栈

May 20, 2016 am 11:39 AM

作者:白狼 出处:http://www.manks.top/article/yii2_umeditor_upload本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

yii2框架整合了百度编辑器,因为文件上传采用的是yii2自带的UploadedFile,这就难免umeditor上传不成功问题,解决问题的只需要两个操作步骤,我们来看看具体实现

首先我们先把umeditor的配置搞好,这里只需要更改imageUrl配置项即可,我们修改其指向/tools/um-upload

那下一步自然是实现/tools/um-upload方法了,

按照ueditor的实现来看,这里我们上传成功后只需要返回成功信息即可

<span style="color: #0000ff;">use</span><span style="color: #000000;"> backend\models\Upload;
</span><span style="color: #0000ff;">use</span><span style="color: #000000;"> yii\web\UploadedFile;
   </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     *    百度umeditor上传
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> actionUmUpload ()
    {
        </span><span style="color: #800080;">$model</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Upload();

        </span><span style="color: #0000ff;">if</span> (Yii::<span style="color: #800080;">$app</span>->request-><span style="color: #000000;">isPost) {

            </span><span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span> = UploadedFile::getInstance(<span style="color: #800080;">$model</span>, &<span style="color: #008000;">#</span><span style="color: #008000;">39;file');</span>

            <span style="color: #800080;">$dir</span> =<span style="color: #000000;"> ‘文件保存目录’;
            </span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">is_dir</span>(<span style="color: #800080;">$dir</span><span style="color: #000000;">))
                </span><span style="color: #008080;">mkdir</span>(<span style="color: #800080;">$dir</span><span style="color: #000000;">);

            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$model</span>-><span style="color: #000000;">validate()) {
                </span><span style="color: #800080;">$fileName</span> = <span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span>-><span style="color: #008080;">baseName</span> . "<span style="color: #008000;">." . $model->file->extension;</span>
                <span style="color: #800080;">$dir</span> = <span style="color: #800080;">$dir</span>."<span style="color: #008000;">/". $fileName;</span>
                <span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span>->saveAs(<span style="color: #800080;">$dir</span><span style="color: #000000;">);

                </span><span style="color: #800080;">$info</span> =<span style="color: #000000;"> [
                    </span>"originalName" => <span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span>-><span style="color: #008080;">baseName</span>,
                    "name" => <span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span>-><span style="color: #008080;">baseName</span>,
                    "url" => <span style="color: #800080;">$dir</span>,
                    "size" => <span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span>->size,
                    "type" => <span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span>->type,
                    "state" => "<span style="color: #008000;">SUCCESS",</span>
<span style="color: #000000;">                ];
                </span><span style="color: #0000ff;">exit</span>(json_encode(<span style="color: #800080;">$info</span><span style="color: #000000;">));
            }  
        }
    }</span>
Copy after login

 

特别提醒:上述返回的$info信息中state状态只能是SUCCESS,区分大小写

关于Yii图片上传请参考Yii2文件上传

关于yii整合百度编辑器请参考yii2整合百度编辑器umeditor

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)