2

I am using Moodle 3.1+. I have used filepicker to upload a file. It is working fine. But filepicker only shows the uploaded file name. I want to show the uploaded image in the filepicker and it seems impossible. Another option is to use filemanager, but it needs extra parameters like contextid which is not there in my case. I am inserting the uploaded image path in a table created by me. So how can I save the file using filemanager into a custom table?

file_save_draft_area_files($data->attachments, $context->id, 'mod_glossary', 'attachment',
                   $entry->id, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50));
Akhilesh
  • 1,243
  • 4
  • 16
  • 49

1 Answers1

1

First, create a filemanager element in your form:

$filemanageropts = $this->_customdata['filemanageropts'];
$mform->addElement('filemanager', 'attachment',get_string('displayedcontent', 'block_helloworld'), null, $filemanageropts);

Note: attachment is the same name from my custom table. Thus, this field will be populated with file itemid.

Then, in you form logic script save file to your custom area :

//form has been submitted
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));

Then, when you load form data : prepare what is called in MOODLE the draft area using:

$draftitemid = $helloworldpage->attachment ; 
        file_prepare_draft_area($draftitemid, $context->id, 'block_helloworld', 'attachment', $helloworldpage->attachment,
            array('subdirs' => 0, 'maxbytes' => 5000000, 'maxfiles' => 1));

Ressources :

ben.IT
  • 1,490
  • 2
  • 18
  • 37
  • 1
    I want to save in my custom table. Here it will save to mdl_files table. – Akhilesh Mar 24 '17 at 10:16
  • 1
    In your custom table you will get the `attachment` field populated with the object id present in `mdl_files` which is in charge to deal with files, so you should be able to manage your file. – ben.IT Mar 24 '17 at 10:47
  • 1
    In which context do I save the file? – Akhilesh Mar 27 '17 at 04:58
  • 1
    @davosmith gives a great [explanation of $context here](http://stackoverflow.com/a/43064593/1632809) – ben.IT Mar 28 '17 at 08:52