Moodle File System - Returning a File ID - Moodle 3.6

Re: Moodle File System - Returning a File ID - Moodle 3.6

by Dave Emsley -
Number of replies: 1
It follows the example one:

public function block_cpd_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
// Check the contextlevel is as expected - if your plugin is a block, this becomes CONTEXT_BLOCK, etc.
if ($context->contextlevel != CONTEXT_BLOCK) {
return false;
}
// Make sure the filearea is one of those used by the plugin.
if ($filearea !== 'attachments') {
return false;
}
// Make sure the user is logged in and has access to the module (plugins that are not course modules should leave out the 'cm' part).
require_login($course, true);

// Leave this line out if you set the itemid to null in make_pluginfile_url (set $itemid to 0 instead).
$itemid = array_shift($args); // The first item in the $args array.

// Use the itemid to retrieve any relevant data records and perform any security checks to see if the
// user really does have access to the file in question.

// Extract the filename / filepath from the $args array.
$filename = array_pop($args); // The last item in the $args array.
if (!$args) {
$filepath = '/'; // $args is empty => the path is '/'
} else {
$filepath = '/'.implode('/', $args).'/'; // $args contains elements of the filepath
}
// Retrieve the file from the Files API.
$fs = get_file_storage();
$file = $fs->get_file($context->id, 'block_cpd', $filearea, $itemid, $filepath, $filename);
if (!$file) {
return false; // The file does not exist.
}

// We can now send the file back to the browser - in this case with a cache lifetime of 1 day and no filtering.
// From Moodle 2.3, use send_stored_file instead.
send_file($file, 86400, 0, $forcedownload, $options);
}
// The PHP tag and the curly bracket for the class definition
// will only be closed after there is another function added in the next section.
}
In reply to Dave Emsley

Re: Moodle File System - Returning a File ID - Moodle 3.6

by Christopher Schönfeldt -

Its exactly my Problem (using M3.8)

Any solution on that? In my case I build a local plugin, but maybe the solution would be the same.