douya6606 2015-01-21 22:38
浏览 341

Summernote - 从服务器删除图像

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?

  • 写回答

3条回答 默认 最新

  • donqo88682 2015-08-24 16:34
    关注

    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.

    评论

报告相同问题?