Displaying an image which has been stored using filemanager

Displaying an image which has been stored using filemanager

by David North -
Number of replies: 5

I am creating a tenant feature, one of the features is that when creating a new tenant, the admin uploads an image for their logo. The logo is then displayed to all users that are assigned to that tenant. I am having trouble with retreiving this stored image, and displaying it in an <img> tag.


I have set up a custom form, which has a filemanager element (not sure if I should be using filepicker). I've managed to get it to store the image and display it in the draft area when editing the record.


 I used this to store the image:

file_save_draft_area_files($data->logo, 1, 'tenant', 'attachment',

                   $data->id, array('subdirs' => 0, 'maxfiles' => 1));


And this to retrieve it in the filemanager when editing the record:

$draftitemid = file_get_submitted_draft_itemid('logo'); 

file_prepare_draft_area($draftitemid, 1, 'tenant', 'attachment', $data->id,

                       array('subdirs' => 0, 'maxfiles' => 1));


This seems to work fine. What I'm having trouble with now is getting the image url and using that to display the image.

I've been trying using this but it says the file does not exist.

$imageurl = moodle_url::make_pluginfile_url(1, 'tenant', 'attachment', $tenantid, '/', 'f1');


What am I doing wrong? I am using moodle 2.8.1



Average of ratings: -
In reply to David North

Re: Displaying an image which has been stored using filemanager

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

I'm not quite clear what plugin you are using (as 'tenant' is not a plugin name - should it be 'mod_tenant' or 'block_tenant'?), but you need to include a function within your plugin to perform the relevant security checks and then serve the file back to the user.

The function must go in your plugin's lib.php file and be called 'PLUGINNAME_pluginfile' (e.g. block_html_pluginfile, mod_assign_pluginfile [you can also use drop the 'mod_' part for activity modules]). As a guide - the 'component' part of the file area should match the first part of the function name.

The function should retrieve the file and then call send_stored_file() to send it back to the browser.

Look at any of the examples in the core modules to see how it works.

In reply to Davo Smith

Re: Displaying an image which has been stored using filemanager

by David North -

Thanks. It's actually a theme I'm adding this functionality to. It's probably not the best way to do it but it seemed easiest for me as I can use one plugin for the theme, this extra feature and a webservice, rather than 3 seperate plugins.

Okay, I didn't realise the PLUGINNAME_pluginfile function was a necessity, I'll add it to my theme lib.php file. I'll also change 'tenant' to 'theme_themename' and give it a try.

In reply to David North

Re: Displaying an image which has been stored using filemanager

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

"Okay, I didn't realise the PLUGINNAME_pluginfile function was a necessity," 

I have spent two evenings missing out that requirement, probably because I didn't read the error message properly sad

In reply to Marcus Green

Re: Displaying an image which has been stored using filemanager

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

It is there in the docs ( https://docs.moodle.org/dev/File_API#Serving_files_to_users ).

The error message wouldn't help you, as it is specifically designed not to give any clue about whether the file exists and you don't have permission to access it, or if the file simply doesn't exist (distinguishing between the two could reveal secret information about your site).

In reply to Davo Smith

Re: Displaying an image which has been stored using filemanager

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Developer, docs, read, moi ????. I had Debug turned to full and it clearly indicated it was trying to use a method that didn't exist, which made me most grateful to whoever designed the API.