简体   繁体   English

Moodle,使用moodle表单上传和存储文件

[英]Moodle, upload and store files using moodle forms

I'm having some dificulties implementing a moodle upload mechanism using moodle forms. 我有一些困难使用moodle表单实现moodle上传机制。 My goal is to let the user/administrator upload images, store them and access later in a block. 我的目标是让用户/管理员上传图像,存储它们并稍后在块中访问。

Currently, I have this in the form: 目前,我有以下形式:

$mform->addElement('filemanager', 'attachments', 'Pic:', null, array('subdirs' => 0, 'maxfiles' => 1,'accepted_types' => '*' ));

and this to save the file: 这是为了保存文件:

if ($draftitemid = file_get_submitted_draft_itemid('attachments')) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_assignment', 'attachments', 0, array('subdirs' => false, 'maxfiles' => 1));
}

and I try to access the file like this: 我尝试访问这样的文件:

file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $context->id . '/mod_assignment/attachments')

I don't receive any errors but I can't access the file either. 我没有收到任何错误,但我也无法访问该文件。 I'm using moodle 2.0. 我正在使用moodle 2.0。

Thanks in advance, Take care 提前谢谢,保重

It sounds like you are looking to write a custom block, in which case you should be giving block_myblock as the component name instead of mod_assignment . 听起来您正在寻找自定义块,在这种情况下,您应该将block_myblock作为组件名称而不是mod_assignment

Each Moodle component which serves files needs to have its own function defined within lib.php to handle file requests. 每个提供文件的Moodle组件都需要在lib.php中定义自己的函数来处理文件请求。 In your case that function wants to be called something like block_myblock_pluginfile() . 在你的情况下,函数想要被调用像block_myblock_pluginfile()

A good example of this is block_html_pluginfile() which can be found in moodle/blocks/html/lib.php and does something very similar to what you are wanting. 一个很好的例子是block_html_pluginfile() ,它可以在moodle / blocks / html / lib.php中找到,并做一些与你想要的非常相似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM