Accessing Course Images in Moodle: Explained and Demonstrated

Accessing Course Images in Moodle: File Upload Methods Explained (Developer Perspective)

In Moodle, file handling is not limited to simple uploads through the user interface. From a coding and plugin-development perspective, Moodle provides multiple file upload mechanisms designed for different use cases. Understanding when to use File Picker, File Manager, and normal file browsing (HTML input) is essential for building stable, scalable, and secure Moodle features.

 1. File Picker (filepicker)

The File Picker is Moodle’s standard file selection interface. It allows users to choose files not only from their local system but also from Moodle repositories such as Private files, Server files, or external repositories (if enabled).

From a coding perspective:

  • Used when you need to select a single file
  • Common in activity settings, form elements, and configuration pages
  • Implemented via filepicker form element in moodleform

Best use case:

  • Course images (single thumbnail or banner)
  • Uploading one document, audio, or video file
  • Settings where replacing a file is expected

Why choose it:
It respects Moodle’s file API, permissions, draft areas, and storage lifecycle automatically.

2. File Manager (filemanager)

The File Manager is designed for handling multiple files and folder structures. It provides a full file-management UI with drag-and-drop support.

From a coding perspective:

  • Used when managing collections of files
  • Works with draft file areas and permanent file storage
  • Supports folders, renaming, deleting, and reordering

Best use case:

  • Course resource libraries
  • Assignment submission areas
  • Activity modules requiring multiple attachments
  • Custom plugins that store grouped files (e.g., lesson assets)

Why choose it:
It gives users flexibility while keeping files securely stored using Moodle’s internal file system instead of the web root.

 3. Normal File Browse (HTML <input type="file">)

This is the basic browser file upload, commonly used in standard web development. Moodle generally discourages this method for core features.

From a coding perspective:

  • Bypasses Moodle’s draft file system
  • Requires manual handling of validation, storage, and security
  • Not integrated with Moodle repositories

Best use case:

  • Temporary uploads
  • AJAX-based tools
  • Custom scripts where Moodle forms are not suitable

Why to avoid for core features:
It increases the risk of security issues and breaks consistency with Moodle’s file lifecycle management.

Which One Should You Use?

  • Use File Picker → When uploading or replacing a single file
  • Use File Manager → When managing multiple files or folders
  • Use normal file browse → Only for special cases, not standard course content

Accessing Course Images in Moodle: Explained and Demonstrated

In Moodle LMS, there are multiple way to access the images uploaded inside the course.
Here in this video we will see two different methods of getting the image in form of URL.

Accessing the URL using the Moodle built-in function

$params = array();
$params = array('id' => $course_id);
$course = $DB->get_record('course', $params, '*', MUST_EXIST);

// here is how you can get the course overview files...
$course = new core_course_list_element($course);
foreach ($course->get_course_overviewfiles() as $file) {
    if ($file->is_valid_image()) {
        $imagepath = '/' . $file->get_contextid() .
                '/' . $file->get_component() .
                '/' . $file->get_filearea() .
                $file->get_filepath() .
                $file->get_filename();
        $imageurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', $imagepath,
                false);
        echo  $imageurl.'<br>';        
        break;
    }
}



Manual/custom way to get the course image path


// Manual method
$context = context_course::instance($course->id, MUST_EXIST);
$files11 = $DB->get_record_sql("select * from {files} where filename!='.' AND contextid = ?
AND  component = ? AND filearea=?", array($context->id, 'course', 'overviewfiles'));

$component = $files11->component;
$filearea = $files11->filearea;
$filename =  $files11->filename;
$itemid = $context->id;
$fullpath = $CFG->wwwroot."/pluginfile.php/$context->id/$component/$filearea/$filename" ;
echo $fullpath ;


Below is the complete code that include the both ways to access the image

<?php

require_once('../config.php');
require_once($CFG->dirroot . '/course/classes/list_element.php');
global $CFG, $DB, $USER, $OUTPUT;
$course_id = optional_param('id', 0, PARAM_INT);
$course_id = 2;

echo $OUTPUT->header();
 
$params = array();
$params = array('id' => $course_id);
$course = $DB->get_record('course', $params, '*', MUST_EXIST);

// here is how you can get the course overview files...
$course = new core_course_list_element($course);
foreach ($course->get_course_overviewfiles() as $file) {
    if ($file->is_valid_image()) {
        $imagepath = '/' . $file->get_contextid() .
                '/' . $file->get_component() .
                '/' . $file->get_filearea() .
                $file->get_filepath() .
                $file->get_filename();
        $imageurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', $imagepath,
                false);
        echo  $imageurl.'<br>';        
        break;
    }
}


// Manual method
$context = context_course::instance($course->id, MUST_EXIST);
$files11 = $DB->get_record_sql("select * from {files} where filename!='.' AND contextid = ?
AND  component = ? AND filearea=?", array($context->id, 'course', 'overviewfiles'));

$component = $files11->component;
$filearea = $files11->filearea;
$filename =  $files11->filename;
$itemid = $context->id;
$fullpath = $CFG->wwwroot."/pluginfile.php/$context->id/$component/$filearea/$filename" ;
echo $fullpath ;

echo $OUTPUT->footer();


You will get the detailed explanation in this video clip





Conclusion

From a developer’s point of view, Moodle’s File Picker and File Manager exist to enforce security, consistency, and scalability. Choosing the correct file upload method ensures your plugin or customization aligns with Moodle standards, performs well, and remains future-proof.

Thank you for being here!

#moodle
#Moodlelms
#lms
#moodlecode
#moodlefile

Post a Comment

Previous Post Next Post