Summernote - 从服务器删除图像 [英] Summernote - Delete image from server

查看:878
本文介绍了Summernote - 从服务器删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经使用以下链接中的代码来允许图像上传到服务器。
Summernote图片上传



如果用户从编辑器中删除图像,是否可以实现类似的操作来从服务器上删除图像?如果是这样,我该如何去实现这一目标?

解决方案

我猜你现在解决了这个问题,但正如我一直在寻找相同的答案,并最终设法让它在这里工作是我的方法:

首先我得到所有 img src 在summernote的 textarea 中,然后我读取服务器上文件夹中的所有 img 文件。之后,我取消链接文件夹中不在summernote的img src数组中的文件。

  $ dom = new domDocument; 
$ dom-> loadHTML(html_entity_decode($ content));
$ dom-> preserveWhiteSpace = false;
$ imgs = $ dom-> getElementsByTagName(img);
$ links = array();
$ path ='../content/'.$id.'/';
$ files = glob($ path。*。*); ($ i = 0; $ i< $ imgs->长度; $ i ++){
$ links [] = $ imgs-> item($ i) - > getAttribute( SRC);
}
$ result = array_diff($ files,$ links);
foreach($ result as $ deleteFile){
if(file_exists($ deleteFile)){
array_map('unlink',glob($ deleteFile));
}
}
}

希望这可以帮助其他人。

编辑回答@eachone:
您也可以使用AJAX(我使用jQuery)。

  $( '#DivContainingSummernoteTextarea')。模糊(函数(){
$ contentPost = $(#SummernoteTextarea ).code();
doAjax('something');
});

我将summernote的textarea的内容存储在 $ contentPost 。然后 doAjax 将调用PHP和POST $ content 。如果服务器上的 img 不在 $ content 中,它将被删除。

Hi I have used the code from the following link to allow images to be uploaded to the server. Summernote image upload

Would it be possible to implement something similar to remove an image from the server if a user deletes it from the editor? If so, how can I go about achieving this?

解决方案

I guess you solved the problem by now but as I have been looking for the same answer and finally managed to make it work here is my approach:

First I get all the img src in summernote's textarea then I read all the img files in the folder on the server. After that, I unlink the files in the folder which are not in summernote's img src array.

        $dom = new domDocument;
        $dom->loadHTML(html_entity_decode($content));
        $dom->preserveWhiteSpace = false;
        $imgs  = $dom->getElementsByTagName("img");
        $links = array();
        $path = '../content/'.$id.'/';
        $files = glob($path . "*.*");
        for($i = 0; $i < $imgs->length; $i++) {
           $links[] = $imgs->item($i)->getAttribute("src");
        }
        $result=array_diff($files, $links);
           foreach($result as $deleteFile) {
               if (file_exists($deleteFile)) {
                 array_map('unlink', glob($deleteFile));
               }
           }
       } 

Hope this will help others.

EDIT to answer to @eachone: You can use AJAX too (I do it with jQuery).

$('#DivContainingSummernoteTextarea').blur(function(){
$contentPost = $("#SummernoteTextarea").code();
doAjax('something');
});

I store the content of summernote's textarea in $contentPost. Then doAjax will call the PHP and POST $content. If the img on the server is not in $content it will be deleted.

这篇关于Summernote - 从服务器删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆