1

In a Moodle form I perform a file upload using the filemanager element:

$mform->addElement('filemanager', 'attachment',get_string('displayedcontent', 'block_helloworld'), null, $filemanageropts);

Once the form is validated, when I record my instance in the database, I also save the uploaded file using the following function:

file_save_draft_area_files($form_submitted_data->attachment, $context->id, 'block_helloworld', 'attachment',
    $form_submitted_data->attachment, array('subdirs' => 0, 'maxbytes' => 500000, 'maxfiles' => 1));

This is working fine but when I take a look at the DB table mdl_files, I saw that for my file there are 4 rows:

component       fileare     itemid  filepath    filename
block_helloworld    attachment  706783489   /   .
block_helloworld    attachment  706783489   /   test5.pdf
user    draft   706783489   /   .
user    draft   706783489   /   test5.pdf

There are 2 rows for my uploaded file in my component block_helloworld and in the component user. One row has a filename but not the other one!

This sounds strange. Is that normal? When I perform file deletion, how to delete all these files?

Note: I am using moodle v3.0.6

ben.IT
  • 1,490
  • 2
  • 18
  • 37

2 Answers2

1

As far as I remember, this is normal behaviour. I had this issue, too, but when you cross check (like doing a file upload into a course) you will notice that there are 2 rows, too. Not sure, why but for me it was normal behaviour

Bearzi
  • 538
  • 5
  • 18
1

The 4 entries are:

  • The folder that your file is in
  • The file itself
  • The folder that the draft version of the file is stored in whilst the form is being edited
  • The draft file whilst the form is being edited

The draft files will be automatically cleaned up after a day or so.

davosmith
  • 6,037
  • 2
  • 14
  • 23
  • ok, when I am implementing the delete page, I only have to remove the 2 rows for my component `block_helloworld` (and do not care about the `user` component rows) ? – ben.IT Mar 23 '17 at 08:51
  • If it's in the block context and you're deleting the block, then Moodle should clean up automatically. If not, just use the files API to delete the area - you should never need to access mdl_files directly. – davosmith Mar 23 '17 at 08:55
  • for sure I will only perform file deletion using the built in file API but when I perform the deletion, I only have the row with the filename that is deleted. The row where filename='.' is still in the mdl_files. Is that normal ? Should I add code using file API to remove this line too ? – ben.IT Mar 23 '17 at 09:31
  • Are you just deleting a single file, or are you calling $fs->delete_area_files(...) to remove the entire area? – davosmith Mar 23 '17 at 13:29
  • I am deleting a single file retrieved from : `$fs->get_area_files(...)` which returns an array of size 1. Thus, the row where filename='.' is still in the `mdl_files` table. Shall I try `$fs->delete_area_files(...)` limited by $itemid ? – ben.IT Mar 23 '17 at 14:03