Upload deleting only 1 file

Upload deleting only 1 file

marianidiegomarianidiego Posts: 50Questions: 15Answers: 1

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

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    Hi,

    What you need to do here is return false; from the dbClean 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 of Database, 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

  • marianidiegomarianidiego Posts: 50Questions: 15Answers: 1

    Would it be possible to have an example please?

  • marianidiegomarianidiego Posts: 50Questions: 15Answers: 1

    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):

        Editor::inst( $db, 'products_image', 'products_image_id' )
                ->field(
                    Field::inst( 'products_image_id' ),
                    Field::inst( 'image' )              
                        ->setFormatter( Format::ifEmpty( null ) )
                        ->upload( 
                            Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/admin/data/products/img/__ID__.__EXTN__' )
                                ->db( 'products_image', 'products_image_id', array(
                                'file'        => Upload::DB_FILE_NAME,
                                'filesize'    => Upload::DB_FILE_SIZE,
                                'web_path'    => Upload::DB_WEB_PATH,
                                'system_path' => Upload::DB_SYSTEM_PATH,
                                'type'        => 'product_image'
                                ))
                                ->dbClean( function ($data) {
    
                                    // Remove the files from the file system
                                    for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
                                        unlink( $data[$i]['system_path'] );
                                    }
    
                                    // Have Editor remove the rows from the database
                                    return false;
                                })
                                ->validator( Validate::fileSize( 5000000, 'Files must be smaller that 5Mb' ) )
                                ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif' ), "Please upload an image" ) )
                    ),
                    Field::inst( 'products_id' ),
                    Field::inst( 'order' )
                )
                ->where( 'products_id', $_POST['products_id'] )
                ->process($_POST)
                ->json();
    
  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    Or, instead of having a separate table with the files, is it possible to save them in the same table?

    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 being product_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

This discussion has been closed.