File Picker

File Picker

by Peter Meint Heida -
Number of replies: 6

How can I make a uploaded file, with the filepicker in edit_form of block, become available for other users. 

After uploading it is stored as a 'draft' file and available for own purpose, but how can I make it available for other users.


I used this code in the renderer.php:

$url = $CFG->wwwroot . "/draftfile.php/" . $file->contextid . '/user/draft/' . $itemid . "/" . $file->filename;

Average of ratings: -
In reply to Peter Meint Heida

Re: File Picker

by Ambrish Tiwari -

Hi Peter,


I am not sure but if you upload the file to a location which is not restricted by the user you might able to provide access to the uploaded files to all the user. For ex if you upload it to \moodle\files\.

Also you can create you own custom block plugin for file upload and specify the path where you want to upload.


Ambrish Tiwari    

In reply to Peter Meint Heida

Re: File Picker

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Peter - I suggest you read carefully through https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms

The basic method is:

  1. Copy any existing files into the draft area ( file_prepare_standard_filemanager() )
  2. Display the form
  3. Once the form has been submitted, copy files into their permanent location ( file_post_update_standard_filemanager() )
  4. Use the file api to get the details of the file, then use moodle_url::make_pluginfile_url() to generate the URL to output
  5. Implement a PLUGINNAME_pluginfile() function in lib.php to authenticate any incoming requests to access that file.



Average of ratings: Useful (1)
In reply to Davo Smith

Re: File Picker

by Peter Meint Heida -

Hi Davo,


Thanks for your response. I read the File_API Moodle forms several times. 

But because it are just peaces. It's hard for me to see it in the complete way, to confirm the draft files the way they become available for all users.


Now my edit_form.php funtion "specific_definition" contains:

// The loop is for uploading an image per course

$courses = $DB->get_records_select('course', 'id > 1');

foreach($courses as $course) {

  $mform->addElement('filepicker', 'config_backgroundimage'.$course->id, get_string('config_backgroundimage', 'block_moduleoverzicht').'<br>Course '.$course->id, null, array('maxbytes' => 1485760, 'accepted_types' => array('.png', '.jpg')));

}

Do you know an excisting example, using the filepicker this way?

In reply to Davo Smith

Re: File Picker

by Raymond Mlambo -
Hello Davo, these numbered points that you've mentioned are very helpful as they shade a bit of light on the File API. Is it possible for me to handle file saving and file serving without the lib.php?

What I mean is, having a separate form and another view**.php file which store and display the file for the users.

In reply to Raymond Mlambo

Re: File Picker

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The standard pluginfile.php script (in the root directory of your Moodle install) that is the usual entry point for serving files, will check to see which plugin the file belongs to, then open up the lib.php script from that plugin and hand over to it to see if the file should be served or not. If lib.php does not exist, or does not contain a PLUGINNAME_pluginfile() function, then it assumes that plugin does not serve any files, so will just return a 'file not found' error.