LMSouq
moodle-core Open

How to store file and retrive it correctly with moodle file api?

AE
AeonDave
1 month ago
3 views
Problem Description
i'm programming a mod activity for moodle which load files and show'em to any student who can access to the course. The problem is that handing files in moodle is damn hard. this is what i have done so far: option page with importers $mform->addElement('filepicker', 'slidesyncmedia', get_string('slidesyncmedia', 'slidesync'), null, array('maxbytes' => $maxbytes, 'accepted_types' => '*')); $mform->addElement('filemanager', 'slidesyncslides', get_string('slidesyncslides', 'slidesync'), null, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50, 'accepted_types' => array('*') )); after submit the files are stored in draft and everything is loaded in another page that save all on db if ($draftitemid = file_get_submitted_draft_itemid('slidesyncmedia')) { file_save_draft_area_files($draftitemid, $context->id, 'mod_slidesync', 'slidesyncmedia', 0, array('subdirs' => 0, 'maxfiles' => 1)); } if ($draftitemid = file_get_submitted_draft_itemid('slidesyncslides')) { file_save_draft_area_files($draftitemid, $context->id, 'mod_slidesync', 'slidesyncslides', 0, array('subdirs' => 0, 'maxfiles' => 50)); } in the end i use again the first page in another place (if files are there, then shows them) $fs = get_file_storage(); if ($files = $fs->get_area_files($context->id, 'mod_slidesync', 'slidesyncslides', '0', 'sortorder', false)) { // Look through each file being managed foreach ($files as $file) { // Build the File URL. Long process! But extremely accurate. $fileurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); echo $fileurl; } } else { echo '<p>Please upload an image first</p>'; } this make an url but if clicked moodle says that file does not exist mysite.com/pluginfile.php/53/mod_slidesync/slidesyncslides/0/Koala.jpg in the db the file are correctly saved!!! 53 mod_slidesync slidesyncslides 0 / Koala.jpg what i'm missing? thanks

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base