Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

Provides the {@link core_form\filetypes_util} class.

Copyright: 2017 David Mudrák <david@moodle.com>
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 561 lines (19 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: filetypes_util  - X-Ref

Utility class for handling with file types in the forms.

This class is supposed to serve as a helper class for {@link MoodleQuickForm_filetypes}
and {@link admin_setting_filetypes} classes.

The file types can be specified in a syntax compatible with what filepicker
and filemanager support via the "accepted_types" option: a list of extensions
(e.g. ".doc"), mimetypes ("image/png") or groups ("audio").

normalize_file_types($types)   X-Ref
Converts the argument into an array (list) of file types.

The list can be separated by whitespace, end of lines, commas, colons and semicolons.
Empty values are not returned. Values are converted to lowercase.
Duplicates are removed. Glob evaluation is not supported.

The return value can be used as the accepted_types option for the filepicker.

return: array of strings
param: string|array $types List of file extensions, groups or mimetypes

looks_like_mimetype($type)   X-Ref
Does the given file type looks like a valid MIME type?

This does not check of the MIME type is actually registered here/known.

return: bool
param: string $type

is_filetype_group($type)   X-Ref
Is the given string a known filetype group?

return: bool|object false or the group info
param: string $type

get_groups_info()   X-Ref
Provides a list of all known file type groups and their properties.

return: array

get_group_description($group)   X-Ref
Return a human readable name of the filetype group.

return: string
param: string $group

describe_file_types($types)   X-Ref
Describe the list of file types for human user.

Given the list of file types, return a list of human readable
descriptive names of relevant groups, types or file formats.

return: object
param: string|array $types

data_for_browser($onlytypes=null, $allowall=true, $current=null)   X-Ref
Prepares data for the filetypes-browser.mustache

return: object
param: string|array $onlytypes Allow selection from these file types only; for example 'web_image'.
param: bool $allowall Allow to select 'All file types'. Does not apply with onlytypes are set.
param: string|array $current Current values that should be selected.

expand($types, $keepgroups=false, $keepmimetypes=false)   X-Ref
Expands the file types into the list of file extensions.

The groups and mimetypes are expanded into the list of their associated file
extensions. Depending on the $keepgroups and $keepmimetypes, the groups
and mimetypes themselves are either kept in the list or removed.

return: array list of extensions and eventually groups and types
param: string|array $types
param: bool $keepgroups Keep the group item in the list after expansion
param: bool $keepmimetypes Keep the mimetype item in the list after expansion

is_listed($types, $list)   X-Ref
Should the file type be considered as a part of the given list.

If multiple types are provided, all of them must be part of the list. Empty type is part of any list.
Any type is part of an empty list.

return: boolean
param: string|array $types File type or list of types to be checked.
param: string|array $list An array or string listing the types to check against.

is_whitelisted($types, $list)   X-Ref
Should the given file type be considered as a part of the given list.

If multiple types are provided, all of them must be part of the
list. Empty type is part of any list. Any type is part of an
empty list.

return: boolean
param: string|array $types File type or list of types to be checked.
param: string|array $list An array or string listing the types to check against.

get_not_listed($types, $list)   X-Ref
Returns all types that are not part of the given list.

This is similar check to the {@see self::is_listed()} but this one actually returns the extra types.

return: array Types not present in the list.
param: string|array $types File type or list of types to be checked.
param: string|array $list An array or string listing the types to check against.

get_not_whitelisted($types, $list)   X-Ref
Returns all types that are not part of the given list.

This is similar check to the {@see self::is_listed()} but this one actually returns the extra types.

return: array Types not present in the list.
param: string|array $types File type or list of types to be checked.
param: string|array $list An array or string listing the types to check against.

is_allowed_file_type($filename, $allowlist)   X-Ref
Is the given filename of an allowed file type?

Empty allowlist is interpreted as "any file type is allowed" rather
than "no file can be uploaded".

return: boolean True if the file type is allowed, false if not
param: string $filename the file name
param: string|array $allowlist list of allowed file extensions

get_unknown_file_types($types)   X-Ref
Returns file types from the list that are not recognized

return: array A list of unknown file types.
param: string|array $types list of user-defined file types