General developer forum

Storing a file in moodle

 
Picture of Robert Payne
Re: Storing a file in moodle
 

Hi Iraklis,

I just came here to ask almost the same thing! Currently I have code that catches the file picker's response and I think it stores it but I'm not sure because I can't get the file again when I go looking for it.

Here's my code so far:

$data = $add_file->get_data();
$contextmodule = context_module::instance($cm->id);
if ($draftitemid = file_get_submitted_draft_itemid('attachmentedfile')) {
      file_save_draft_area_files($draftitemid, $contextmodule->id, 'mod_mediaassignment', 'attachmentedfile', 0, array('subdirs' => false, 'maxfiles' => 1));
}
$content = $add_file->get_file_content('attachmentedfile'); $name = $add_file->get_new_filename('attachmentedfile');
print_object($add_file->get_file_content('attachmentedfile'));
$success = $add_file->save_file('attachmentedfile', 'submission', false);
$url = moodle_url::make_pluginfile_url($contextmodule->id, 'mod_mediaassignment', 'submission', $data->attachmentedfile, null, $name);

The save_file() function doesn't seem to return anything and the make_plugin_url() produces a url that isn't giving me a file so I'm clearly doing things wrong. 

$content is returning the data from inside the file and $name is showing the file name so I've know I've got it good up until then.

 
Average of ratings: -