Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

File API: Difference between revisions

From MoodleDocs
m (Add more examples)
Line 59: Line 59:


* See [[Using_the_File_API_in_Moodle_forms|Using the File API in Moodle forms]]
* See [[Using_the_File_API_in_Moodle_forms|Using the File API in Moodle forms]]
==Examples==
===Browsing files===
<code php>
$browser = get_file_browser();
$context = get_system_context();


==Moving files around==
$filearea = null;
$itemid  = null;
$filename = null;
if ($fileinfo = $browser->get_file_info($context, $filearea, $itemid, '/', $filename)) {
    // build a Breadcrumb trail
    $level = $fileinfo->get_parent();
    while ($level) {
        $params = base64_encode(serialize($level->get_params()));
        $path[] = array('name'=>$level->get_visible_name(), 'path'=>$params);
        $level = $level->get_parent();
    }
    $path = array_reverse($path);
    $children = $fileinfo->get_children();
    foreach ($children as $child) {
        if ($child->is_directory()) {
            echo $child->get_visible_name();
            // display contextid, itemid, filepath and filename
            var_dump($child->get_params());
        }
    }
}
 
</code>
===Moving files around===


For example, if you have just built a file at the path
For example, if you have just built a file at the path
 
<code php>
  $from_zip_file = $CFG->dataroot . '/temp/backup/' . $preferences->backup_unique_code .
  $from_zip_file = $CFG->dataroot . '/temp/backup/' . $preferences->backup_unique_code .
         '/' . $preferences->backup_name;
         '/' . $preferences->backup_name;
 
</code>
And you want to move it into the course_backup file area, do
And you want to move it into the course_backup file area, do
 
<code php>
  $context = get_context_instance(CONTEXT_COURSE, $preferences->backup_course);
  $context = get_context_instance(CONTEXT_COURSE, $preferences->backup_course);
  $fs = get_file_storage();
  $fs = get_file_storage();
Line 75: Line 103:
         'timecreated'=>time(), 'timemodified'=>time());
         'timecreated'=>time(), 'timemodified'=>time());
  $fs->create_file_from_pathname($file_record, $from_zip_file);
  $fs->create_file_from_pathname($file_record, $from_zip_file);
</code>
===Create a copy of stored file===
If you need to create a copy of stored file (actually, it add a new record in database):
<code php>
// $oldfile is an instance of stored_file
$now = time();
$context = get_context_instance(CONTEXT_USER, $USER->id);
$recored = new stdclass;
$record->filearea = 'user_draft';
$record->contextid = $context->id;
$record->filename  = 'file name.txt';
$record->filepath  = '/';
$record->timecreated  = $now;
$record->timemodified = $now;
$record->userid      = $USER->id;
$record->mimetype = $oldfile->get_mimetype();
if (!empty($itemid)) {
    $record->itemid  = $itemid;
}
$newfile = $fs->create_file_from_storedfile($record, $oldfile->get_id());
</code>


=== List area files ===
<code php>
$fs = get_file_storage();
$files = $fs->get_area_files($contextid, 'user_draft');
foreach ($files as $f) {
    // $f is an instance of stored_file
    echo $f->get_filename();
}
</code>
==See also==
==See also==



Revision as of 06:34, 20 May 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 File API. The page is just about what you need to know to use the file API. Related is the 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;

Naming file areas

It is important that file areas are named consistently so we do not get name collisions, and so the names are easy to understand. Please follow the following guidelines:

start of the name

If the file area belongs to a plugin, please use the plugin name as the start of the file area name.

This is the same plugin name that you would use for get_string calls. Some examples:

  • All file areas that belong to the forum modules should have a name beginning with 'forum'. For example 'forum_post', 'forum_intro'.
  • A file area belonging to the HTML block would start 'block_html_'.
  • A file area belonging to a question type would start 'qtype_myqtype_', except this is probably not necessary, because question type images should probably be stored in the core 'question_text' file area.
  • If the file area is used by a local hack, the file area name should start 'local_'.

If the file area belongs to core code, the file area name should start with a prefix that indicates what part of Moodle it belongs to.

But try to avoid clashing with prefixes that a plugin might use. Some examples:

  • 'course_intro'
  • 'question_text'
  • 'user_draft' (although draft file areas are a special case).

rest of the name

Like naming variables or functions, try to find a name that is short, but says exactly what the file area is for.

If possible use the name of the file area to give a clue as to which database table the itemid relates to. For example:

  • For the 'forum_post' file area, the itemid links to forum_post.id.
  • For 'question_text' file area, the itemid links to question.id. (Would it be permissible to call this file area just question?)

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

Examples

Browsing files

$browser = get_file_browser(); $context = get_system_context();

$filearea = null; $itemid = null; $filename = null; if ($fileinfo = $browser->get_file_info($context, $filearea, $itemid, '/', $filename)) {

   // build a Breadcrumb trail
   $level = $fileinfo->get_parent();
   while ($level) {
       $params = base64_encode(serialize($level->get_params()));
       $path[] = array('name'=>$level->get_visible_name(), 'path'=>$params);
       $level = $level->get_parent();
   }
   $path = array_reverse($path);
   $children = $fileinfo->get_children();
   foreach ($children as $child) {
       if ($child->is_directory()) {
           echo $child->get_visible_name();
           // display contextid, itemid, filepath and filename
           var_dump($child->get_params());
       }
   }

}

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);

Create a copy of stored file

If you need to create a copy of stored file (actually, it add a new record in database): // $oldfile is an instance of stored_file

$now = time(); $context = get_context_instance(CONTEXT_USER, $USER->id); $recored = new stdclass; $record->filearea = 'user_draft'; $record->contextid = $context->id; $record->filename = 'file name.txt'; $record->filepath = '/'; $record->timecreated = $now; $record->timemodified = $now; $record->userid = $USER->id; $record->mimetype = $oldfile->get_mimetype(); if (!empty($itemid)) {

   $record->itemid   = $itemid;

} $newfile = $fs->create_file_from_storedfile($record, $oldfile->get_id());

List area files

$fs = get_file_storage(); $files = $fs->get_area_files($contextid, 'user_draft'); foreach ($files as $f) {

   // $f is an instance of stored_file
   echo $f->get_filename();

}

See also