It is possible to delete a file if there is no reference from it in the database using mysql

Asked

Viewed 192 times

2

I wonder if anyone knows any way to create a "trigger" in Mysql that when deleting a table value, delete a file together on the server, for example:

  1. Excludes the item from tbl_imagens whose name is imagem_1001.jpg;
  2. The trigger takes the value imagem_1001.jpg and deletes in the directory uploads/imagem_1001.jpg

Obs:

  • I don’t care to save images in the database;
  • I don’t care to delete with PHP unlink();
  • 1

    Do you really need to delete a file through Mysql? You cannot use the programming language?

  • 1

    I don’t see much logic in doing this for the bank, and I don’t know if there’s a possibility. As you are working with a programming language make this exclusion with it, will be more coherent.

  • Yes, and searching, I saw people commenting that can insert linux commands in the trigger. RM for example;

  • Yes, there is a lot of logic in this, you will be avoiding many conditions and queries in a given system that makes use of a complex image chain

  • 1

    An alternative would be to create a table of imagens_para_excluir. This table could be run from a sheel script that queries this table, removes the files from the disk and truncates that table. You could schedule the execution of this script via cronjob.

  • 1

    I agree with the comment of @gmsantos. Separation of concepts: Database is for storing data, Filesystem for managing files. Some other layer of the solution should do the file purge.

  • Managed to solve your problem?

Show 2 more comments
No answers

Browser other questions tagged

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