如何使用Froala编辑器上传图像?

时间:2019-10-08 09:14:46

标签: laravel froala

我有一个laravel 5.5项目,我正在尝试使用froala编辑器上传图像,这是脚本:

 $(function() {

        $('.selector').froalaEditor({

            enter: $.FroalaEditor.ENTER_P,

            placeholderText: null,

            height: 500,

            fileUploadURL: "{{ URL::to('administrator/froala-upload-file') }}",

            imageUploadURL: "{{ URL::to('administrator/froala-upload') }}",

            imageUploadParams: {

                id: 'edit_fr'

            }
        })

    });

这是我在控制器中上传的功能:

 public function uploadImage()
{

    // Allowed extentions.
    $allowedExts = array("gif", "jpeg", "jpg", "png", "blob");
    //dd($_FILES);
    // Get filename.
    $temp = explode(".", $_FILES["file"]["name"]);

    // Get extension.
    $extension = end($temp);
    $name = sha1(microtime()) . "." . $extension;

    // An image check is being done in the editor but it is best to
    // check that again on the server side.
    // Do not use $_FILES["file"]["type"] as it can be easily forged.
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $_FILES["file"]["tmp_name"]);

    if ((($mime == "image/gif")
    || ($mime == "image/jpeg")
    || ($mime == "image/pjpeg")
    || ($mime == "image/x-png")
    || ($mime == "image/png"))
    && in_array(strtolower($extension), $allowedExts)) {

        // Save file in the uploads folder.
        move_uploaded_file($_FILES["file"]["tmp_name"], getcwd() . "/uploads/froala/images/" . $name);

        // Generate response.
        $response = new StdClass;
        $response->link = "//127.0.0.1/isc/uploads/froala/images/" . $name;
        echo stripslashes(json_encode($response));
    }

    $path = "//127.0.0.1/isc/uploads/froala/images/" . $name;

    $img = New Imageget();
    $img->url = $path;
    $img->thumb = $path;
    $img->name = $name;
    $img->type = 'image';
    $img->tag = $mime;
    $img->save();
}

我在控制台中收到“ 500(内部服务器错误)”,当我检查错误时,它显示“未定义的索引:文件”

0 个答案:

没有答案