yii2 solves the problem of image upload in Baidu editor umeditor ewebeditor editor download metaeditor editor download ewebeditor editor upgrade

WBOY
Release: 2016-07-29 08:51:56
Original
1258 people have browsed it

Author: Bailang Source: http://www.manks.top/article/yii2_umeditor_upload The copyright of this article belongs to the author, and you are welcome to reprint it. However, this statement must be retained without the author's consent, and a link to the original text must be provided in an obvious position on the article page. Otherwise, we reserve the right to pursue legal liability.

The yii2 framework integrates the Baidu editor. Because the file upload uses the UploadedFile that comes with yii2, it is inevitable that the umeditor upload will not be successful. To solve the problem, only two steps are needed. Let’s take a look at the specific implementation

First of all Let’s complete the configuration of ueditor first. Here we only need to change the imageUrl configuration item. We modify it to point to /tools/um-upload

Then the next step is to implement the /tools/um-upload method.

Follow ueditor From the perspective of implementation, here we only need to return success information after the upload is successful

<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&#39;);</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>
Copy after login

Special reminder: the state in the $info information returned above can only be SUCCESS, which is case-sensitive

Please refer to Yii image upload Yii2 file upload

For information about yii integrating Baidu editor, please refer to yii2 integrating Baidu editor umeditor

The above introduces how yii2 solves the image upload problem of Baidu editor umeditor, including the content of editor and Baidu editor. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!