File upload problem

File upload problem

SarbastSarbast Posts: 78Questions: 0Answers: 0

Hello,

I have a form for teacher activities which contain upload file. The form work fine , but sometimes the file uploaded and display in the form and after click sumit the file and its info in the db removed. As I said this happen sometimes. I used custom upload file , but the problem does not solve.

My code and db is as this example
https://editor.datatables.net/examples/advanced/upload.html

Regards

Replies

  • colincolin Posts: 14,526Questions: 1Answers: 2,482

    Odd. When it does happen, are you seeing console errors in the browser, or any server-side errors, either in the database or web server logs?

    Colin

  • SarbastSarbast Posts: 78Questions: 0Answers: 0

    Hello Colin,

    the data stored in the activities table, so there is no error. Also, the image_id field in the activities table take a value from images table and after submission the row remove from images table.

  • allanallan Posts: 57,236Questions: 1Answers: 9,113 Site admin

    Do you have a dbClean function defined on the server-side? Also, are multiple people accessing and modifying data at the same time?

    Allan

  • SarbastSarbast Posts: 78Questions: 0Answers: 0

    Hello Allan,

    Yes, I have dbClean in server-side and more than people accessing on the same time.

  • allanallan Posts: 57,236Questions: 1Answers: 9,113 Site admin

    I suspect that might be what is happening in that case. If dbClean runs it will find orphaned records. So if you happen to upload a file, then someone else edits a row before you submit your form, it will find your file as an orphan and delete it.

    Do you have a "created" field in the database for the file? If so, you could add a check into it to make sure that it only deletes orphaned files which are older than a day which would resolve that issue.

    Allan

  • SarbastSarbast Posts: 78Questions: 0Answers: 0

    Hello Allan,

    I have "Uploaded_Date" field in the activities table. If you mean in the images table, I don't have. I will add it and let you know.

    Thanks Allan

  • SarbastSarbast Posts: 78Questions: 0Answers: 0
    edited April 28

    Hello Allan,

    I added created field as "date" to the images table and add check if the date equal a day or older then delete file as below, but the problem is in the last line "return true" which will delete all orphaned files info from database.

    Here is my code

    ->dbClean( function ( $data ) {
                    // Remove the files from the file system
                    for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
                  $start_date = strtotime($data[$i]['date']);
                          $end_date = strtotime(date('Y-m-d'));
                  $diff = ($end_date - $start_date)/60/60/24;
                        if ( $diff >= 1 ){
                            unlink( $data[$i]['system_path'] ); 
                }
                      }
              return true;
                     } )
    

    Thanks

  • SarbastSarbast Posts: 78Questions: 0Answers: 0

    Sorry this is the correct one.

    I added created field as "date" to the images table and add check if the date equal a day or older then delete file as below, but the problem is in the last line "return true" which will delete all orphaned files info from database.

    Here is my code

    ->dbClean( function ( $data ) {
                    // Remove the files from the file system
                    for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
                $file_date = date_create($data[$i]['date']);
                $current_date = date_create(date('Y-m-d'));
                $diff = date_diff($file_date, $current_date);
                        if ( $diff->format("%a") >= 1 ){
                            unlink( $data[$i]['system_path'] ); 
                            }
                      }
            // Have Editor remove the rows from the database
               return true;
         } )
    

    Thanks

  • SarbastSarbast Posts: 78Questions: 0Answers: 0

    Hello Allan,

    I solved as follow. Is this correct or not?

    ->dbClean( function ( $data ) use ($db){
                   // Remove the files from the file system and rows from the database
                    for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
                  $file_date = date_create($data[$i]['date']);
                  $current_date = date_create(date('Y-m-d'));
                  $diff = date_diff($file_date, $current_date);
                        if ( $diff->format("%a") >= 1 ){
                            unlink( $data[$i]['system_path'] ); 
                $db->raw()->bind( ':id', $data[$i]['id'] )->exec( 'DELETE FROM activities_docs WHERE id = :id' );
                }
                         }
                 } )
    

    Regards

  • allanallan Posts: 57,236Questions: 1Answers: 9,113 Site admin

    Hi,

    Yes, that looks like a good solution to me. I must admit I'd thought dbClean was called once per file, but that obviously isn't the case, apologies. I'll have a look at adding something to make this kind of thing easier in future!

    Until then, your solution looks spot on.

    Regards,
    Allan

  • SarbastSarbast Posts: 78Questions: 0Answers: 0

    Thanks Allan,

    Also, In my system the people upload files alot, so for my case which is better the default upload as in the upload file example or custom upload?

    Thanks again

  • allanallan Posts: 57,236Questions: 1Answers: 9,113 Site admin

    Either could work fine. If the default upload does what you need, then I'd go with that. Only if you need to customise it would you need the custom upload.

    Allan

  • SarbastSarbast Posts: 78Questions: 0Answers: 0

    Thanks alot Allan

Sign In or Register to comment.