How can I get an uploaded file URL from the hash number?

How can I get an uploaded file URL from the hash number?

by Matt Bury -
Number of replies: 7
Picture of Plugin developers

Hi,

I'm developing a mod for Moodle 2.4. I've looked around, tried out a few suggestions, looked at the docs and looked at some other Moodle mods and don't seem to be getting anywhere very quickly.

In my mod/swf/mod_form.php, I've got the upload file input for an *.xml file:

        $filemanager_options2['accepted_types'] = '*'; // I'll change this when it's all working to accept XML, SMIL, MPEG-7, and SubRip.
        $filemanager_options2['maxbytes'] = 0;
        $filemanager_options2['maxfiles'] = -1;
        $filemanager_options2['mainfile'] = true;
        $mform->addElement('filemanager', 'xmlurl', get_string('xmlurl', 'swf'), null, $filemanager_options2);

This works fine and inserts hash numbers into the DB, e.g. 98602295.

Can anyone point me in the direction of the particular function(s) and/or objects to use to retrieve a valid URL, from these hash numbers?

e.g. http://localhost/m24/pluginfile.php/26/mod_swf/content/2/photo.jpg

Thanks in advance! smile

Average of ratings: -
In reply to Matt Bury

Re: How can I get an uploaded file URL from the hash number?

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

Files in the filemanager are stored in a 'draft' area, whilst they are being edited on the form (so you can click 'cancel' and get back your original file list).

You need to copy the current files into the draft area, display the form, then (if the form is submitted) copy the files back out to the 'real' area. Then, you can use moodle_url::make_pluginfile_url to generate a suitable URL (in combination with mod_swf_pluginfile() in lib.php to check the security and return the file).

There is an example here: https://github.com/davosmith/filemanager which covers most of the basics of setting up a filemanager and retrieving the files.

In reply to Davo Smith

Re: How can I get an uploaded file URL from the hash number?

by Matt Bury -
Picture of Plugin developers

Thanks, I'll have a look at those.

In reply to Davo Smith

Re: How can I get an uploaded file URL from the hash number?

by Matt Bury -
Picture of Plugin developers

Hi again,

I couldn't make much sense of the examples. There's less information there than from looking at existing modules.

So far, no joy. Is there any comprehensive documentation yet? i.e. somewhere that has all the necessary information to make uploading and embedding/downloading files within plugin modules in one place. Is anyone working on it?

If not, which Moodle 2.4 module has the clearest example of the URL of a mod_form.php upload being printed out in a view.php page?

In reply to Matt Bury

Re: How can I get an uploaded file URL from the hash number?

by Andrew Normore -

I totally agree with you Matt, filemanager is VERY confusing, and poorly documented.  

What's important to understand is that a generic file manager is EASY to create. It's just a form element. Once you drag and drop something, it appears to work.

What actually happens, is the file get put in /user/draft/.... and really isn't very useful. So you have to do a LOT of work to get it working. You actually have to tell the form (on submit) to save it to the proper area, in the correct context.

 

   // Save the files submitted
    file_save_draft_area_files($draftitemid, $context->id, 'local_filemanager', 'attachment', $itemid, $filemanageropts);

This bit of code here is what saves the file from the users draft area, to an actual useful place.

 

functionlocal_filemanager_pluginfile(

This function which is REQUIRED to be in a lib.php is something Moodle scans for, and uses by default. This function is responsible for outputting an image once it's uploaded. You'll see this function all over the place in Moodle.

 

EDIT: Haha! Cool! Davo forked the file manager example I made. Neeeaaat.

In reply to Andrew Normore

Re: How can I get an uploaded file URL from the hash number?

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

Andrew - and I also (at the time) put in a pull request in case you wanted to integrate my changes back in to your version smile

 

In reply to Davo Smith

Re: How can I get an uploaded file URL from the hash number?

by Andrew Normore -

Ahh no kidding. 

I'd think I'll spend a bit more time cleaning it up today, and making it SUPER noob friendly. It'll be kinda neat to have been able to contribute to Moodle.