Note: You are currently viewing documentation for Moodle 3.3. Up-to-date documentation for the latest stable version of Moodle is probably available here: Using the File API.

Development:Using the File API: Difference between revisions

From MoodleDocs
No edit summary
No edit summary
Line 1: Line 1:
{{Moodle 2.0}}
{{Moodle 2.0}}
The File API is for managing all the files stored by Moodle. If you are interested in how the file API works internally, see [[Development:File_API]]. The page is just about what you need to know to use the file API. Related is the [[Development:Repository API]], which lets users get files into Moodle.
The File API is for managing all the files stored by Moodle. If you are interested in how the file API works internally, see [[Development:File API]]. The page is just about what you need to know to use the file API. Related is the [[Development:Repository API]], which lets users get files into Moodle.


==File areas==
==File areas==

Revision as of 06:09, 1 April 2009

Template:Moodle 2.0 The File API is for managing all the files stored by Moodle. If you are interested in how the file API works internally, see Development:File API. The page is just about what you need to know to use the file API. Related is the Development:Repository API, which lets users get files into Moodle.

File areas

Files are are conceptually stored in file areas. A file area is uniquely identified by:

  • A context id.
  • A file area type, for example 'course_intro' or 'forum_post'.
  • A unique itemid. Normally, the itemid relates to something depending on the file area type. For example, for a 'course_intro' file area, the itemid is is the course id. For forum post, it is the post id.

File areas are not listed separately anywhere, they are stored implicitly in the files table. So, to get a list of all the file areas in your Moodle (that contain at least one file), use SELECT DISTINCT contextid, filearea, itemid FROM mdl_file; and to get a list of all the file area types, use SELECT DISTINCT filearea FROM mdl_file;

Serving files to users

You must refer to the file with a URL that includes a file-serving script, often pluginfile.php. For example $url = $CFG->wwwroot/pluginfile.php/$contextid/$filearea/$itemid/file/path.ext; Often you get these URLs generated automatically for you using the function file_rewrite_pluginfile_urls.

Getting files from the user

Moving files around

For example, if you have just built a file at the path

$from_zip_file = $CFG->dataroot . '/temp/backup/' . $preferences->backup_unique_code .
        '/' . $preferences->backup_name;

And you want to move it into the course_backup file area, do

$context = get_context_instance(CONTEXT_COURSE, $preferences->backup_course);
$fs = get_file_storage();
$file_record = array('contextid'=>$context->id, 'filearea'=>'course_backup',
        'itemid'=>0, 'filepath'=>'/', 'filename'=>$preferences->backup_name,
        'timecreated'=>time(), 'timemodified'=>time());
$fs->create_file_from_pathname($file_record, $from_zip_file);

See also