文章内容

2017/9/12 17:24:50,作 者: 黄兵

summernote php上传图片

前面写了使用summernote 使用java上传图片和ASP.NET MVC 使用summernote 富文本编辑器请参考这里:summernote asp.net mvc实现图片上传

现在我们看一下如何使用php在summernote里面上传图片,直接上代码:

<script>
    $(document).ready(function() {
        $('#summernote').summernote({
            height: 200,
            onImageUpload: function(files, editor, welEditable) {
                sendFile(files[0], editor, welEditable);
            }
        });
        function sendFile(file, editor, welEditable) {
            data = new FormData();
            data.append("file", file);
            $.ajax({
                data: data,
                type: "POST",
                url: "Your URL POST (php)",
                cache: false,
                contentType: false,
                processData: false,
                success: function(url) {
                    editor.insertImage(welEditable, url);
                }
            });
        }
    });


</script>

PHP

if ($_FILES['file']['name']) {
            if (!$_FILES['file']['error']) {
                $name = md5(rand(100, 200));
                $ext = explode('.', $_FILES['file']['name']);
                $filename = $name . '.' . $ext[1];
                $destination = '/assets/images/' . $filename; //change this directory
                $location = $_FILES["file"]["tmp_name"];
                move_uploaded_file($location, $destination);
                echo 'http://test.yourdomain.al/images/' . $filename;//change this URL
            }
            else
            {
              echo  $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['file']['error'];
            }
        }

如果对上面的代码有什么疑问,请进群

 summernote 群(群号:127375427

黄兵的个人博客原创。

转载请注明出处:黄兵个人博客 - summernote php上传图片

分享到:

发表评论

评论列表