Moodle LMS

Accessing Course Images in Moodle: Explained and Demonstrated

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

In Moodle,nfile handling is not limited to simple uploads through the user interface. Fromna coding and plugin-development perspective, Moodle provides multiplenfile upload mechanisms designed for different use cases. Understanding when tonuse File Picker, File Manager, and normal file browsing (HTMLninput) is essential for building stable, scalable, and secure Moodlenfeatures.

 1. FilenPicker (filepicker)

The FilenPicker is Moodle’s standard file selection interface. It allows users tonchoose files not only from their local system but also from Moodlenrepositories such as Private files, Server files, or external repositoriesn(if enabled).

From ancoding perspective:

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

Bestnuse case:

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

Whynchoose it:
nIt respects Moodle’s file API, permissions, draft areas, and storage lifecyclenautomatically.

2. FilenManager (filemanager)

The FilenManager is designed for handling multiple files and foldernstructures. It provides a full file-management UI with drag-and-drop support.

 

From ancoding perspective:

    • Used when managing collectionsn of files
    • Works with draft file areasn and permanent file storage
    • Supports folders, renaming,n deleting, and reordering

Bestnuse case:

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

Whynchoose it:
nIt gives users flexibility while keeping files securely stored using Moodle’sninternal file system instead of the web root.

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

This isnthe basic browser file upload, commonly used in standard webndevelopment. Moodle generally discourages this method for core features.

From ancoding perspective:

    • Bypasses Moodle’s draft filen system
    • Requires manual handling ofn validation, storage, and security
    • Not integrated with Moodlen repositories

Bestnuse case:

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

Why tonavoid for core features:
nIt increases the risk of security issues and breaks consistency with Moodle’snfile lifecycle management.

WhichnOne Should You Use?

    • Use File Picker → When uploading or replacingn a single file
    • Use File Manager → When managing multiplen files or folders
    • Use normal file browse → Only for special cases,n 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

Leave a Reply

Your email address will not be published. Required fields are marked *