File areas

File areas

by Pedro Remedios -
Number of replies: 5
After reading the little documentation on file areas, I still don't know what it is.

When I call the function send_stored_file (I think) to save an uploaded file to a file area of my plugin, I can't find the place to see if it successfully put the file in the file area. Also if I need to delete a file from a file area, where can I do that?


Average of ratings: -
In reply to Pedro Remedios

Re: File areas

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
It might help if I give a slightly higher-level explanation.
A file path in Moodle (and thus the arguments passed to most filesystem functions) consists of the following:
  • component: The part of the system or plugin the files belong to, e.g. user or mod_assign
    • contextid: The instance of the component the files belong to, e.g. the user's context ID, or the context ID of the mod_assign instance
      • filearea: is an arbitrarily named area within the context that contains a type of files, e.g. "draft" or "submission".
        • itemid: an ID that is used to uniquely identify a set of files (an "item") within the filearea.  This is usually the ID from a database table related to the uploaded files, e.g. the ID of the assign_submission record.
          • path and filename: Within the item, you can have multiple files within a sort-of virtual directory, addressed by path and filename. If you just uploaded a single file to a filemanager, the path will be '/' and the filename will be the original name of the uploaded file.

send_stored_file() isn't for saving an uploaded file, it's for sending a file to the browser (e.g. for download).

If you want to save a file uploaded by a form, you want file_save_draft_area_files(). 

You can delete a file by getting the stored_file object that represents the file (usually by doing get_file_storage() to get the file_storage object, and calling one of its get_*() methods), then calling the delete() method.

In reply to Mark Johnson

Re: File areas

by Pedro Remedios -

So uploading a file to a plugin's file area is the same as saving the file contents to a field in the database? So a file area is a place in a table in the database? So that means I can't perform maintenance on the file area without coding?

In reply to Pedro Remedios

Re: File areas

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

The content of the files is stored on the disk in the 'filedir' directory, within your site's dataroot.

But this content is only given a location in the front end + meaning due to its entry in the mdl_files database table. Additionally, each set of file content is only stored once (so, if you upload a 500mb video, then rename it and upload it again, Moodle only stores the 500mb content once, but includes 2 references to it in the mdl_files database table).


Average of ratings: Useful (1)