我在moodle中创建一个表单。我需要在enctype属性中multipart/form-data
来处理文件。但我不知道应该在哪里设置属性。以及如何设置它。
答案 0 :(得分:0)
使用Forms API查看Files API以上传文件。
http://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms
更新:Haven未对此进行测试,但它是我现有代码的缩减版本
在你的edit.php文件中有这样的内容:
require_once(dirname(__FILE__) . '/edit_form.php');
$id = optional_param('id', null, PARAM_INT);
if ($id) {
// Existing record
$record = $DB->get_record('mytablename');
} else {
// New record
$record = new stdClass();
$record->id = null;
$record->myfield = '';
...
}
// Prepare the files.
$fileoptions = array(
'maxbytes' => get_max_upload_file_size(),
'maxfiles' => '1', // Change this to how many files can be uploaded.
'subdirs' => 0, // No sub directories.
'context' => $context
);
$record = file_prepare_standard_filemanager($record, 'myfiles',
$fileoptions, $context, 'mypluginname', 'myfiles', $record->id);
$mform = new edit_form(null, array('fileoptions' => $fileoptions));
if ($formdata = $mform->get_data()) {
// Form has been submitted.
if (empty($formdata->id)) {
// New record so create it.
$recordid = $DB->insert_record('mytablename', $formdata);
$record = $DB->get_record('mytablename', array('id' => $recordid));
} else {
// Update it.
$DB->update_record('mytablename', $formdata);
$record = $DB->get_record('mytablename', array('id' => $id));
}
// Save the files.
$formdata = file_postupdate_standard_filemanager($formdata, 'myfiles',
$fileoptions, $context, 'mypluginname', 'myfiles', $record->id);
} else {
// Display the form.
$mform->set_data($record);
$mform->display();
}
在你的edit_form.php中有这样的东西
defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir . '/formslib.php');
class edit_form extends moodleform {
public function definition() {
$fileoptions = $this->_customdata['fileoptions'];
$mform =& $this->_form;
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
...
$mform->addElement('filemanager', 'myfiles_filemanager',
get_string('myfiles', 'mypluginname'), null, $fileoptions);
$this->add_action_buttons(false, get_string('submit'));
}
}