Delete image file while deleting from tinymce editor

Asked

Viewed 77 times

-2

I’m using Tinymce, to edit the website text and also upload images.

The upload and its appearance is working perfectly.

Editor

Question

It is possible to send some GET command with the image name so that it can also be deleted in the directory?

tinymce.init({
  selector: 'textarea',
  directionality: 'pt_BR',
  language: 'pt_BR',
  height: 400,
  menubar: true,
  plugins: [
    " advlist anchor autolink codesample image imagetools",
    " lists link media noneditable preview",
    " searchreplace table visualblocks wordcount paste code fullscreen"
  ],
  toolbar: "undo redo | bold underline italic | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist | image paste | code | fullscreen",
  paste_as_text: true,
  paste_data_images: true,
  entity_encoding: "raw",
  images_upload_url: './upload_tinymce.php'
});

1 answer

0

If understood (the question is confused) you want to delete an image of the server that is embedded inside tinymce chance the user remove it from the editing area?

I must say that this is a bad idea, this because these types of editors support Ctrl+Z, so if you delete the moment you remove the image from the editing area and the user decides to do a Ctrl+Z it will no longer have the image of the server, it may be until it is cached in the browser, but on the server side it will not exist.

The solution I propose is to simply solve this in the back end at the moment you save the data on the server, it is basically simple, by sending the contents of tinymce to the server you can use the DomDocument PHP to parse in HTML, but this will only work if this editor is for a one thing, if that’s not the case then what you should do is check every issue users have made, assuming you have a table with something like that (let’s call table "posts"):

id title content
1 title 1 foo bar <img src="baz.jpg"> um
2 title 2 lorem y <img src="bar.jpg"> dois

Then in the time to save you can check all "posts" and get all images (all this is hypothetical) and do the DOM parse of all this to get all images (follows pseudocode for understanding, independent of PDO or mysqli or other databases):

if (!query('INSERT INTO postagens')) // OU UPDATE, depende de como você preparou a lógica
{
     die('Falhou ao salvar');
}

function pegar_imagens($doc)
{
     $imagens = array();

     forach ($doc->getElementsByTagName('img') as $img)
     {
          $path = $img->getAttribute('src');

          if (!$path) continue;

          $imagens[] = basename($path);
     }

     return $imagens;
}

$todas_imagens = array();

$data = query('SELECT conteudo FROM postagens');

while ($row = $data->fetch()) {
    $doc = new DOMDocument;
    $doc->loadHTML($row['conteudo']); //Faz o parse do HTML no php

    //Junta imagens de postagens diferentes no mesmo array
    $todas_imagens = array_merge($todas_imagens, pegar_imagens($doc));
}

//Remove imagens repetidas
$todas_imagens = array_unique($todas_imagens);

//Pega todas imagens da pasta no seu servidor
foreach (glob('upload/*.{jpg,png,gif,webp}', GLOB_BRACE) as $imagem)
{
      //Verifica se a imagem da pasta NÃO está em nenhuma postagem
      if (!in_array(basename($imagem), $todas_imagens)) {
           //Acaso não estiver deleta a imagem
           unlink($imagem);
      }
}

It is worth remembering that if many users are editing posts this can cause conflicts, the way I believe it would help to mitigate, if really have more than one user using the system is to separate the images for each user, could separate by folders, then the glob() should be executed only in the folder that belongs to the specific user.

Another way would be to run the above script in a CRON (without the Insert and update part, since it is something totally separate), this of course should be done at an idle time, at a time when preferably no one is using the editor, this you could check through your logins system (if it was well worked out) or then set a time when the system enters maintenance mode and prevents access only to the text editor, then you would schedule the CRON to run every day at this X time, could even put the "cleaning" for a Sunday, example:

0 03 * * * /var/www/tarefas/limpar-imagens-em-desuso.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.