class file_support

internal  
 

File storage utility wrapper for Moodle's File Storage API.

This class centralizes file operations using Moodle's file_storage system, providing a consistent interface for file management across the plugin.

All file operations should go through this class to ensure proper handling of Moodle's file storage architecture and to maintain consistency.

Constants

COMPONENT

Methods

static file_storage
get_storage()

Retrieves the Moodle file storage instance.

static false|stored_file|null
get_file(int $contextid, string $component, string $filearea, int $itemid, bool $validimage = false)

Retrieves a single file from a file area.

static array
get_area_files(int $contextid, string $component, string $filearea, int $itemid)

Retrieves all files from a file area (excluding directories).

static stored_file|null
get_file_by_path(int $contextid, string $component, string $filearea, int $itemid, string $filepath, string $filename)

Retrieves a specific file by its path components.

static stored_file|null
get_file_by_id(int $fileid)

Retrieves a file by its database ID.

static stored_file|null
create_file_from_stored(stored_file $sourcefile, int $contextid, string $component, string $filearea, int $itemid, string $filepath = '/', string|null $filename = null)

Creates a file record from an existing stored file (copy).

static stored_file|null
create_file_from_string(int $contextid, string $component, string $filearea, int $itemid, string $filepath, string $filename, string $content)

Creates a file from raw string content.

static stored_file|null
create_file_from_pathname(int $contextid, string $component, string $filearea, int $itemid, string $filepath, string $filename, string $pathname)

Creates a file from a local filesystem path.

static bool
delete_file(stored_file $file)

Deletes a specific file.

static bool
delete_file_by_id(int $fileid)

Deletes a file by its ID.

static bool
delete_area_files(int $contextid, string $component, string $filearea, false|int $itemid = false)

Deletes all files in a specific file area (optionally limited by itemid).

static string
get_file_url(stored_file $file, bool $forcedownload = false, string|null $preview = null)

Retrieves the URL for a stored file.

static bool
has_files(int $contextid, string $component, string $filearea, int $itemid)

Checks if a file area contains any files.

static int
get_area_size(int $contextid, string $component, string $filearea, int $itemid)

Calculates the total size of files in a file area.

static int
count_area_files(int $contextid, string $component, string $filearea, int $itemid)

Counts the number of files in a file area.

static bool
is_valid_image(stored_file $file)

Validates if a file is a valid image.

static string|null
get_content(stored_file $file)

Retrieves file content as a string.

static null|resource
get_content_file_handle(stored_file $file)

Retrieves file content as a resource handle.

Details

at line 48
static file_storage get_storage()

Retrieves the Moodle file storage instance.

Return Value

file_storage

File storage instance

at line 66
static false|stored_file|null get_file(int $contextid, string $component, string $filearea, int $itemid, bool $validimage = false)

Retrieves a single file from a file area.

Useful for areas that should contain only one file (e.g., profile pictures, logos).

Parameters

int $contextid

Context ID

string $component

Component name (e.g., 'local_middag')

string $filearea

File area name

int $itemid

Item ID

bool $validimage

If true, only return valid image files

Return Value

false|stored_file|null

File object, false if validation fails, null if not found

at line 105
static array get_area_files(int $contextid, string $component, string $filearea, int $itemid)

Retrieves all files from a file area (excluding directories).

Parameters

int $contextid

Context ID

string $component

Component name

string $filearea

File area name

int $itemid

Item ID

Return Value

array

Array of stored file objects

at line 142
static stored_file|null get_file_by_path(int $contextid, string $component, string $filearea, int $itemid, string $filepath, string $filename)

Retrieves a specific file by its path components.

Parameters

int $contextid

Context ID

string $component

Component name

string $filearea

File area name

int $itemid

Item ID

string $filepath

File path (e.g., '/')

string $filename

File name

Return Value

stored_file|null

File object or null if not found

at line 169
static stored_file|null get_file_by_id(int $fileid)

Retrieves a file by its database ID.

Parameters

int $fileid

File ID

Return Value

stored_file|null

File object or null if not found

at line 198
static stored_file|null create_file_from_stored(stored_file $sourcefile, int $contextid, string $component, string $filearea, int $itemid, string $filepath = '/', string|null $filename = null)

Creates a file record from an existing stored file (copy).

Useful for duplicating files or moving them between areas.

Parameters

stored_file $sourcefile

Source file to copy

int $contextid

Target context ID

string $component

Target component

string $filearea

Target file area

int $itemid

Target item ID

string $filepath

Target file path (default: '/')

string|null $filename

Target filename (null to keep original)

Return Value

stored_file|null

Created file or null on failure

at line 242
static stored_file|null create_file_from_string(int $contextid, string $component, string $filearea, int $itemid, string $filepath, string $filename, string $content)

Creates a file from raw string content.

Parameters

int $contextid

Context ID

string $component

Component name

string $filearea

File area name

int $itemid

Item ID

string $filepath

File path (default: '/')

string $filename

File name

string $content

File content

Return Value

stored_file|null

Created file or null on failure

at line 283
static stored_file|null create_file_from_pathname(int $contextid, string $component, string $filearea, int $itemid, string $filepath, string $filename, string $pathname)

Creates a file from a local filesystem path.

Parameters

int $contextid

Context ID

string $component

Component name

string $filearea

File area name

int $itemid

Item ID

string $filepath

File path in storage (default: '/')

string $filename

File name in storage

string $pathname

Local file system path

Return Value

stored_file|null

Created file or null on failure

at line 318
static bool delete_file(stored_file $file)

Deletes a specific file.

Parameters

stored_file $file

File to delete

Return Value

bool

True on success, false otherwise

at line 336
static bool delete_file_by_id(int $fileid)

Deletes a file by its ID.

Parameters

int $fileid

File ID

Return Value

bool

True on success, false otherwise

at line 363
static bool delete_area_files(int $contextid, string $component, string $filearea, false|int $itemid = false)

Deletes all files in a specific file area (optionally limited by itemid).

Parameters

int $contextid

Context ID

string $component

Component name

string $filearea

File area name

false|int $itemid

Item ID

Return Value

bool

True on success, false otherwise

at line 387
static string get_file_url(stored_file $file, bool $forcedownload = false, string|null $preview = null)

Retrieves the URL for a stored file.

Parameters

stored_file $file

File object

bool $forcedownload

Force download instead of display

string|null $preview

Preview mode ('thumb', 'tinyicon', 'bigicon')

Return Value

string

File URL

at line 438
static bool has_files(int $contextid, string $component, string $filearea, int $itemid)

Checks if a file area contains any files.

Parameters

int $contextid

Context ID

string $component

Component name

string $filearea

File area name

int $itemid

Item ID

Return Value

bool

True if files exist, false otherwise

at line 472
static int get_area_size(int $contextid, string $component, string $filearea, int $itemid)

Calculates the total size of files in a file area.

Parameters

int $contextid

Context ID

string $component

Component name

string $filearea

File area name

int $itemid

Item ID

Return Value

int

Total size in bytes

at line 504
static int count_area_files(int $contextid, string $component, string $filearea, int $itemid)

Counts the number of files in a file area.

Parameters

int $contextid

Context ID

string $component

Component name

string $filearea

File area name

int $itemid

Item ID

Return Value

int

Number of files

at line 520
static bool is_valid_image(stored_file $file)

Validates if a file is a valid image.

Parameters

stored_file $file

File to validate

Return Value

bool

True if valid image, false otherwise

at line 538
static string|null get_content(stored_file $file)

Retrieves file content as a string.

Parameters

stored_file $file

File object

Return Value

string|null

File content or null on failure

at line 556
static null|resource get_content_file_handle(stored_file $file)

Retrieves file content as a resource handle.

Parameters

stored_file $file

File object

Return Value

null|resource

File handle or null on failure