【问题标题】:Filemanager save file into custom table文件管理器将文件保存到自定义表中
【发布时间】:2017-03-23 22:36:17
【问题描述】:

我正在使用 Moodle 3.1+。我已经使用文件选择器上传文件。它工作正常。但文件选择器只显示上传的文件名。我想在文件选择器中显示上传的图像,这似乎是不可能的。另一种选择是使用文件管理器,但它需要额外的参数,如我的情况下不存在的 contextid。我正在将上传的图像路径插入我创建的表中。那么如何使用文件管理器将文件保存到自定义表中呢?

file_save_draft_area_files($data->attachments, $context->id, 'mod_glossary', 'attachment',
                   $entry->id, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50));

【问题讨论】:

    标签: php moodle


    【解决方案1】:

    首先,在表单中创建一个文件管理器元素:

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

    注意:附件与我的自定义表格中的名称相同。因此,该字段将填充文件 itemid。

    然后,在您的自定义区域中形成逻辑脚本保存文件:

    //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));
    

    然后,当您加载表单数据时:准备在 MOODLE 中调用的草稿区域:

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

    资源:

    【讨论】:

    • 我想保存在我的自定义表格中。在这里它将保存到 mdl_files 表中。
    • 在您的自定义表格中,您将获得attachment 字段,其中填充了mdl_files 中负责处理文件的对象ID,因此您应该能够管理您的文件。
    • 在什么情况下保存文件?
    • @davosmith 给了一个很棒的explanation of $context here
    猜你喜欢
    • 1970-01-01
    • 2020-04-24
    • 2014-12-11
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多