Development:Using the File API: Difference between revisions
No edit summary |
|||
Line 1: | Line 1: | ||
{{Moodle 2.0}} | {{Moodle 2.0}} | ||
The | 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 | |||
<code sql> | |||
SELECT DISTINCT contextid, filearea, itemid FROM mdl_file; | |||
</code> | |||
and to get a list of all the file area types, use | |||
<code sql> | |||
SELECT DISTINCT filearea FROM mdl_file; | |||
</code> | |||
==Serving files to users== | |||
You must refer to the file with a URL that includes a file-serving script, often pluginfile.php. For example | |||
<code php> | |||
$url = $CFG->wwwroot/pluginfile.php/$contextid/$filearea/$itemid/file/path.ext; | |||
</code> | |||
Often you get these URLs generated automatically for you using the function file_rewrite_pluginfile_urls. | |||
==Getting files from the user== | ==Getting files from the user== | ||
* See [[Development:Using_the_File_API_in_Moodle_forms|Using the File API in Moodle forms]] | |||
==Moving files around== | ==Moving files around== | ||
Line 37: | Line 48: | ||
==See also== | ==See also== | ||
* [[Development: | * [[Development:File API]] how the File API works internally. | ||
* [[Roadmap|Moodle 2.0 roadmap]] | * [[Roadmap|Moodle 2.0 roadmap]] | ||
[[Category:Developer]] | [[Category:Developer]] | ||
[[Category:Files]] | [[Category:Files]] |
Revision as of 06:08, 1 April 2009
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
- Development:File API how the File API works internally.
- Moodle 2.0 roadmap