Upload deleting only 1 file
Upload deleting only 1 file
Link to test case: https://editor.datatables.net/manual/php/upload
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem: I use a single table for all uploads. So multiple tables will reference this one table. When I delete a row, I would like that one file to be deleted from the server.
I can't use dbClean() as in the example, because it would leave me only the files used in the current table deleting all the others. In the instructions I find written:
> (note this only checks the host Editor table, not any other database tables - so if you have multiple tables using the same files table, you will need to execute your own queries). The information provided in the passed in array is defined by the options specified for the Upload->db() method.
Basically how can I do this? Where do I find instructions on how to use this Upload->db(), do you have an example I can copy from?
Thank you very much
Answers
Hi,
What you need to do here is
return false;
from thedbClean
callback function - that will stop Editor from making any changes to the database for the files table, letting you have full control over it.From there, what you need to do is query the database (you could use the
$db
variable which is an instance ofDatabase
, documented here, or just use PDO directly) to determine which files are orphaned (i.e. do a join to the other tables which reference them and find out which are no longer referenced). Those records can then be deleted and the files removed.I'm afraid we don't have a running example of that in use.
Regards,
Allan
Would it be possible to have an example please?
Or, instead of having a separate table with the files, is it possible to save them in the same table?
Example type (this example does not work):
No sorry. The way Editor's upload works is that the file is async from the rest of the form. When you select a file it is immediately uploaded to the server and processed / stored. Typically that means a new entry in a database row which the main form can then link to.
Thinking about this there might be a route to make the dbClean more useful for you. Do all the files you want to be considered for removal here have something in common (the
type
beingproduct_image
) for example?What I'm thinking is that I can add a condition to the method which is used to get the ids to delete - i.e. only get the ids which have
type = 'product_image'
in this case. This is the relevant part of the code if you are interested.Regards,
Allan