upload (many) & deleting

upload (many) & deleting

marianidiegomarianidiego Posts: 61Questions: 18Answers: 1

Link to test case:
https://editor.datatables.net/examples/advanced/upload-many.html

I use Editor 2.0.7.

Debugger code (debug.datatables.net):

Error messages shown:

Description of problem:
I have craeato of the tables like in the example https://editor.datatables.net/examples/advanced/upload-many.html, for the upload all well. It works perfectly. If I delete the files, however, is not deleted the file in the server.... and is not deleted the line in the database in the table "files".

I tried to put some parameters "ON DELETE CASCADE" between the tables "files" and "users_files", but I have other problems (upload generates an error).

I tried to use the "dbClean" function, not present in the official example, but nothing changes.

I can't figure out how the file deletion works, and I'm stuck. Any idea?


Nel mio software, le tabelle hanno dei nomi diversi. Vi mando il mio codice, se questo possa essere di aiuto....

                    ...
                    ->join(
                        Mjoin::inst( 'machines_data' )
                            ->link( 'machines.machines_id', 'machines_data_item.machines_id' )
                            ->link( 'machines_data.machines_data_id', 'machines_data_item.machines_data_id' )
                            ->fields(
                                Field::inst( 'machines_data_id' )
                                    ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'] .'/admin/data/machines/__ID__.__EXTN__' )
                                        ->db( 'machines_data', 'machines_data_id', array(
                                            'file'        => Upload::DB_FILE_NAME,
                                            'filesize'    => Upload::DB_FILE_SIZE,
                                            'web_path'    => Upload::DB_WEB_PATH,
                                            'system_path' => Upload::DB_SYSTEM_PATH
                                        ))
                                        ->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 true;
                                        })
                                        ->validator( Validate::fileSize( 5000000, 'Files must be smaller that 5Mb' ) )
                                        ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif', 'pdf', 'zip' ), "Please upload a png, jpg, jpeg, gir, pdf or zip" ) )
                                    )
                            )
                    )
                    ...


                            ...
                            fields: [ {
                                    label: "ID TEST:",
                                    name: "machines_story.machines_story_id",
                                    type:  "hidden"
                                },{
                                    label: "Notes:",
                                    name: "machines_story.machines_id",
                                    type:  "hidden",
                                    def: rowData.machines.machines_id
                                },{
                                    label: "Start Time:",
                                    name: "machines_story.time_start",
                                    type:      'datetime',
                                    def:   function () { return null },
                                    format: 'DD-MM-YYYY h:mm',
                                    fieldInfo: 'Euro style date with 24 hour clock',
                                    opts: {
                                        minutesAvailable: [ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 ]
                                    }
                                },{
                                    label: "Stop Time:",
                                    name: "machines_story.time_stop",
                                    type:      'datetime',
                                    def:   function () { return new Date(); },
                                    format: 'DD-MM-YYYY h:mm',
                                    fieldInfo: 'Euro style date with 24 hour clock',
                                    opts: {
                                        minutesAvailable: [ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 ]
                                    }
                                },{
                                    label: "Text:",
                                    name: "machines_story.text",
                                    type: "ckeditorClassic"
                                },
                                {
                                    label: "Files",
                                    name: "machines_story_data[].machines_story_data_id",
                                    type: "uploadMany",
                                    display: function ( file_id ) {

                                                var filename = childEditor2.file( 'machines_story_data', file_id ).file;
                                                var extention = filename.split('.').pop();

                                                if("pdf" == extention) 
                                                        return '<img src="dist/img/pdf.png"/> ' + filename;
                                                if("zip" == extention) 
                                                        return '<img src="dist/img/zip.png"/> ' + filename;

                                                return '<img src="' + childEditor2.file( 'machines_story_data', file_id ).web_path + '"/> ' + filename;

                                            },
                                    clearText: "Clear",
                                    noImageText: 'No Data'
                                }
                            ]
                            ...

    { title: 'Files',       "data": "machines_story_data",      "width": '50px',
                              render: function ( data, type, row ) {
                                var stringa = "";
                                data.forEach(function (item, index){

                                    var value =item['machines_story_data_id'];

                                    stringa += '<a href="dist/dl.php?t=story&f=' + childTable2.file( 'machines_story_data', value ).web_path.split('/').pop() + '&c=' + "<?=$csrf?>" + '&n=' + childTable2.file( 'machines_story_data', value ).file + '" target="_blank">';

                                    var extention = childTable2.file( 'machines_story_data', value ).file.split('.').pop();
                                    if("pdf" == extention){
                                        stringa += '<i class="fas fa-file-pdf"></i>';
                                    }
                                    else if("zip" == extention){
                                        stringa += '<i class="fas fa-file-archive"></i>';
                                    } else {
                                        stringa += '<i class="fas fa-file-image"></i>';
                                    }
                                    stringa += ' </a>';

                                });

                                return stringa;
                              }
                            }
                            ...

Answers

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin

    That looks like it should work okay. Are you getting any error messages, or just nothing happening? Is the unlink function call actually running? I think the first step would be to put a debug statement in there to make sure it is running and against the files you expect. Likewise check that the dbClean callback is running.

    Allan

  • marianidiegomarianidiego Posts: 61Questions: 18Answers: 1
    edited March 2022

    I ran the tests you mentioned. I modified the code as follows:

                                          ->dbClean( function ($data) {
                                                     error_log("unlink: " . $data[$i]['system_path'], 0);
                                                // Remove the files from the file system
                                                for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
                                                    error_log("unlink: " . $data[$i]['system_path'], 0);
                                                    unlink( $data[$i]['system_path'] );
                                                }
    
                                                // Have Editor remove the rows from the database
                                                return true;
                                            })
    

    But I do not have it saved in the error log of PHP. The function is not called. What can it be?

  • marianidiegomarianidiego Posts: 61Questions: 18Answers: 1
    edited March 2022

    Solved!

    dbClean in the multyupload should be named like this:

        ->dbClean(**'machines_data_item.machines_data_id'**, 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 true;
        }) 
    

    For the official website example it would be:

    ->dbClean(**'users_files.file_id'**, 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 true;
    }) 
    
  • jyd0228jyd0228 Posts: 30Questions: 11Answers: 0

    Thanks to you, I solved it. Thank you.

Sign In or Register to comment.