Get the filename of uploaded file via File Manager

Get the filename of uploaded file via File Manager

by Matt Sawatzky -
Number of replies: 4

Hey Guys,

Moodle Version: 3.7.1

So my code to save the file that was uploaded using the file manager form element follows the documentation.

Form Definition for this Element

$mform->addElement('filemanager', 'thumbnailfile', get_string('video_thumbnail', 'mod_ovivideogallery'), null, 
array('accepted_types' => array('.png','.jpg'), 'return_types'=> FILE_INTERNAL | FILE_EXTERNAL));


Handling Form Submit with Uploaded File

file_save_draft_area_files($data->thumbnailfile, $systemcontext->id, 'mod_mycomponent', 
'thumbnailfile', $data->id, array('maxfiles' => 1));

--

This works.. I can see the file data in the database, and I can load that image back up into the draft area when loading the form again.
What I can't figure out how to do is when I want to get that image link, I need to pass the filename in the
moodle_url::make_pluginfile_url() function.
When I hard code the filename in, it works, but the filename won't always be the same. So how do I get the filename on the initial form submit so I can store that in the database to use it later to get the image?

Or is there a different way of doing it that I'm not seeing. I can't find anything in the documentation or in other modules.

Thanks,

Matt

Average of ratings: -
In reply to Matt Sawatzky

Re: Get the filename of uploaded file via File Manager

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

You want to use the Moodle files API to retrieve the uploaded file(s) - https://docs.moodle.org/dev/File_API

It will look something like this:

$fs = get_file_storage();
$files = $fs->get_area_files($systemcontext->id, 'mod_mycomponent', 'thumbnailfile', $data->id, '', false);
$file = reset($files);
if ($file) {
    $url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(),
                                           $file->get_filearea(), $file->get_filepath(), $file->get_filename());
}

There are probably typos in there, because I'm writing this without an IDE to check function names against. Note the two final params for get_area_files are the sort order ('' in this case, as we only expect 1 file to be returned) and whether or not to include sub directories (no, in this case).

Doing it this way means you get the filename (and all other details) from the stored_file object returned by get_area_files - really there should probably be a version of make_pluginfile_url() that accepts a stored_file object directly, but no one's got around to providing that.

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

Re: Get the filename of uploaded file via File Manager

by Dave Emsley -
As I understand it there should be a $file->get_itemid() in there too.

I jumped on this thread as it seems to do what I need but I can't get it to return a URL that works.   Further detail in https://moodle.org/mod/forum/discuss.php?d=259070 is very helpful too but still not returning a useable file URL.

Cheers

Dave


In reply to Dave Emsley

Re: Get the filename of uploaded file via File Manager

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

Yes, I missed the itemid part - good catch.

In what way is the URL not working? Can you confirm whether or not it is finding your PLUGINNAME_pluginfile() function inside your plugin's lib.php script? The easiest way to debug this is using xdebug and a breakpoint in pluginfile.php.

If you can't set up xdebug for some reason, the I suggest you put die('got here'); at the top of your pluginfile function and view the generated URL directly in the browser. If that works, then the problem lies in your function, if not then there is something wrong with the URL so it is not matching back to your plugin.

In reply to Davo Smith

Re: Get the filename of uploaded file via File Manager

by Matt Sawatzky -
Thanks Davo! The get_area_files method was exactly what I was looking for!