Moodle PHP Documentation 4.4
Moodle 4.4.1 (Build: 20240610) (db07c09afc5)
|
File storage class used for low level access to stored files. More...
Public Member Functions | |
__construct () | |
Constructor - do not use directly use get_file_storage() call instead. | |
add_file_to_pool ($pathname, $contenthash=null, $newrecord=null) | |
Add file content to sha1 pool. | |
add_string_to_pool ($content, $newrecord=null) | |
Add string content to sha1 pool. | |
content_exists ($contenthash) | |
Content exists. | |
convert_image ($filerecord, $fid, $newwidth=null, $newheight=null, $keepaspectratio=true, $quality=null) | |
Creates new image file from existing. | |
create_directory ($contextid, $component, $filearea, $itemid, $filepath, $userid=null) | |
Recursively creates directory. | |
create_file_from_pathname ($filerecord, $pathname) | |
Add new local file. | |
create_file_from_reference ($filerecord, $repositoryid, $reference, $options=array()) | |
Create a new alias/shortcut file from file reference information. | |
create_file_from_storedfile ($filerecord, $fileorid) | |
Add new local file based on existing local file. | |
create_file_from_string ($filerecord, $content) | |
Add new local file. | |
create_file_from_url ($filerecord, $url, array $options=null, $usetempfile=false) | |
Add new local file. | |
cron () | |
Cron cleanup job. | |
delete_area_files ($contextid, $component=false, $filearea=false, $itemid=false) | |
Delete all area files (optionally limited by itemid). | |
delete_area_files_select ($contextid, $component, $filearea, $itemidstest, array $params=null) | |
Delete all the files from certain areas where itemid is limited by an arbitrary bit of SQL. | |
delete_component_files ($component) | |
Delete all files associated with the given component. | |
file_exists ($contextid, $component, $filearea, $itemid, $filepath, $filename) | |
Does this file exist? | |
file_exists_by_hash ($pathnamehash) | |
Whether or not the file exist. | |
get_area_files ($contextid, $component, $filearea, $itemid=false, $sort="itemid, filepath, filename", $includedirs=true, $updatedsince=0, $limitfrom=0, $limitnum=0) | |
Returns all area files (optionally limited by itemid) | |
get_area_tree ($contextid, $component, $filearea, $itemid) | |
Returns array based tree structure of area files. | |
get_converted_document (stored_file $file, $format, $forcerefresh=false) | |
Get converted document. | |
get_directory_files ($contextid, $component, $filearea, $itemid, $filepath, $recursive=false, $includedirs=true, $sort="filepath, filename") | |
Returns all files and optionally directories. | |
get_external_files ($repositoryid, $sort='') | |
Returns all files belonging to given repository. | |
get_file ($contextid, $component, $filearea, $itemid, $filepath, $filename) | |
Fetch locally stored file. | |
get_file_by_hash ($pathnamehash) | |
Fetch file using local file full pathname hash. | |
get_file_by_id ($fileid) | |
Fetch file using local file id. | |
get_file_instance (stdClass $filerecord) | |
Create instance of file class from database record. | |
get_file_preview (stored_file $file, $mode) | |
Returns an image file that represent the given stored file as a preview. | |
get_file_system () | |
Return the file system instance. | |
get_references_by_storedfile (stored_file $storedfile) | |
Returns all aliases that link to the given stored_file. | |
get_references_count_by_storedfile (stored_file $storedfile) | |
Returns the number of aliases that link to the given stored_file. | |
get_unused_dirname ($contextid, $component, $filearea, $itemid, $suggestedpath) | |
Return an available directory name. | |
get_unused_filename ($contextid, $component, $filearea, $itemid, $filepath, $filename) | |
Return an available file name. | |
get_user_draft_items (int $userid, int $updatedsince=0, int $lastnum=0) | |
Returns the file area item ids and their updatetime for a user's draft uploads, sorted by updatetime DESC. | |
import_external_file (stored_file $storedfile, $maxbytes=0) | |
Convert file alias to local file. | |
is_area_empty ($contextid, $component, $filearea, $itemid=false, $ignoredirs=true) | |
Are there any files (or directories) | |
move_area_files_to_new_context ($oldcontextid, $newcontextid, $component, $filearea, $itemid=false) | |
Move all the files in a file area from one context to another. | |
search_references ($reference) | |
Returns all aliases that refer to some stored_file via the given reference. | |
search_references_count ($reference) | |
Returns the number of aliases that refer to some stored_file via the given reference. | |
search_server_files ($query, $from=0, $limit=20, $count=false) | |
Search through the server files. | |
setup_file_system () | |
Complete setup procedure for the file_system component. | |
supports_xsendfile () | |
Returns true if filesystem is configured to support xsendfile. | |
synchronise_stored_file_from_file (stored_file $file, $path, $filerecord) | |
Synchronise stored file from file. | |
synchronise_stored_file_from_string (stored_file $file, $content, $filerecord) | |
Synchronise stored file from string. | |
try_content_recovery ($file) | |
Tries to recover missing content of file from trash. | |
update_references ($referencefileid, $lastsync, $lifetime, $contenthash, $filesize, $status, $timemodified=null) | |
Updates a reference to the external resource and all files that use it. | |
update_references_to_storedfile (stored_file $storedfile) | |
Updates all files that are referencing this file with the new contenthash and filesize. | |
xsendfile ($contenthash) | |
Serve file content using X-Sendfile header. | |
xsendfile_file (stored_file $file) | |
Serve file content using X-Sendfile header. | |
Static Public Member Functions | |
static | can_convert_documents () |
Check if the installed version of unoconv is supported. | |
static | get_pathname_hash ($contextid, $component, $filearea, $itemid, $filepath, $filename) |
Calculates sha1 hash of unique full path name information. | |
static | hash_from_path ($filepath) |
Calculate and return the contenthash of the supplied file. | |
static | hash_from_string ($content) |
Calculate and return the contenthash of the supplied content. | |
static | mimetype ($fullpath, $filename=null) |
Return mimetype by given file pathname. | |
static | mimetype_from_file ($fullpath) |
Inspect a file on disk for it's mimetype. | |
static | pack_reference ($params) |
When user referring to a moodle file, we build the reference field. | |
static | send_test_pdf () |
Regenerate the test pdf and send it direct to the browser. | |
static | test_unoconv_path () |
Check if unoconv configured path is correct and working. | |
static | unpack_reference ($str, $cleanparams=false) |
Unpack reference field. | |
Protected Member Functions | |
call_before_file_created_plugin_functions ($newrecord, $pathname=null, $content=null) | |
before_file_created hook. | |
create_file ($newrecord) | |
Add new file record to database and handle callbacks. | |
create_file_preview (stored_file $file, $mode) | |
Generates a preview image for the stored file. | |
create_imagefile_preview (stored_file $file, $mode) | |
Generates a preview for the stored image file. | |
is_format_supported_by_unoconv ($format) | |
Verify the format is supported. | |
sort_area_tree ($tree) | |
Sorts the result of file_storage::get_area_tree(). | |
File storage class used for low level access to stored files.
Only owner of file area may use this class to access own files, for example only code in mod/assignment/* may access assignment attachments. When some other part of moodle needs to access files of modules it has to use file_browser class instead or there has to be some callback API.
file_storage::add_file_to_pool | ( | $pathname, | |
$contenthash = null, | |||
$newrecord = null ) |
Add file content to sha1 pool.
string | $pathname | path to file |
string | null | $contenthash | sha1 hash of content if known (performance only) |
stdClass | null | $newrecord | New file record |
array | (contenthash, filesize, newfile) |
file_storage::add_string_to_pool | ( | $content, | |
$newrecord = null ) |
Add string content to sha1 pool.
string | $content | file content - binary string |
array | (contenthash, filesize, newfile) |
|
protected |
before_file_created hook.
stdClass | null | $newrecord | New file record. |
string | null | $pathname | Path to file. |
string | null | $content | File content. |
|
static |
Check if the installed version of unoconv is supported.
bool | true if the present version is supported, false otherwise. |
file_storage::content_exists | ( | $contenthash | ) |
file_storage::convert_image | ( | $filerecord, | |
$fid, | |||
$newwidth = null, | |||
$newheight = null, | |||
$keepaspectratio = true, | |||
$quality = null ) |
Creates new image file from existing.
stdClass | array | $filerecord | object or array describing new file |
int | stored_file | $fid | file id or stored file object |
int | $newwidth | in pixels |
int | $newheight | in pixels |
bool | $keepaspectratio | whether or not keep aspect ratio |
int | $quality | depending on image type 0-100 for jpeg, 0-9 (0 means no compression) for png |
stored_file |
file_storage::create_directory | ( | $contextid, | |
$component, | |||
$filearea, | |||
$itemid, | |||
$filepath, | |||
$userid = null ) |
Recursively creates directory.
int | $contextid | context ID |
string | $component | component |
string | $filearea | file area |
int | $itemid | item ID |
string | $filepath | file path |
int | $userid | the user ID |
stored_file|false | success |
|
protected |
Add new file record to database and handle callbacks.
stdClass | $newrecord |
file_storage::create_file_from_pathname | ( | $filerecord, | |
$pathname ) |
Add new local file.
stdClass | array | $filerecord | object or array describing file |
string | $pathname | path to file or content of file |
stored_file |
file_storage::create_file_from_reference | ( | $filerecord, | |
$repositoryid, | |||
$reference, | |||
$options = array() ) |
Create a new alias/shortcut file from file reference information.
stdClass | array | $filerecord | object or array describing the new file |
int | $repositoryid | the id of the repository that provides the original file |
string | $reference | the information required by the repository to locate the original file |
array | $options | options for creating the new file |
stored_file |
file_storage::create_file_from_storedfile | ( | $filerecord, | |
$fileorid ) |
Add new local file based on existing local file.
stdClass | array | $filerecord | object or array describing changes |
stored_file | int | $fileorid | id or stored_file instance of the existing local file |
stored_file | instance of newly created file |
file_storage::create_file_from_string | ( | $filerecord, | |
$content ) |
Add new local file.
stdClass | array | $filerecord | object or array describing file |
string | $content | content of file |
stored_file |
file_storage::create_file_from_url | ( | $filerecord, | |
$url, | |||
array | $options = null, | ||
$usetempfile = false ) |
Add new local file.
stdClass | array | $filerecord | object or array describing file |
string | $url | the URL to the file |
array | $options | download_file_content() options |
bool | $usetempfile | use temporary file for download, may prevent out of memory problems |
stored_file |
|
protected |
Generates a preview image for the stored file.
stored_file | $file | the file we want to preview |
string | $mode | preview mode, eg. 'thumb' |
stored_file|bool | the newly created preview file or false |
|
protected |
Generates a preview for the stored image file.
stored_file | $file | the image we want to preview |
string | $mode | preview mode, eg. 'thumb' |
string|bool | false if a problem occurs, the thumbnail image data otherwise |
file_storage::delete_area_files | ( | $contextid, | |
$component = false, | |||
$filearea = false, | |||
$itemid = false ) |
Delete all area files (optionally limited by itemid).
int | $contextid | context ID |
string | $component | component |
string | $filearea | file area or all areas in context if not specified |
int | $itemid | item ID or all files if not specified |
bool | success |
file_storage::delete_area_files_select | ( | $contextid, | |
$component, | |||
$filearea, | |||
$itemidstest, | |||
array | $params = null ) |
Delete all the files from certain areas where itemid is limited by an arbitrary bit of SQL.
int | $contextid | the id of the context the files belong to. Must be given. |
string | $component | the owning component. Must be given. |
string | $filearea | the file area name. Must be given. |
string | $itemidstest | an SQL fragment that the itemid must match. Used in the query like WHERE itemid $itemidstest. Must used named parameters, and may not used named parameters called contextid, component or filearea. |
array | $params | any query params used by $itemidstest. |
file_storage::delete_component_files | ( | $component | ) |
Delete all files associated with the given component.
string | $component | the component owning the file |
file_storage::file_exists | ( | $contextid, | |
$component, | |||
$filearea, | |||
$itemid, | |||
$filepath, | |||
$filename ) |
Does this file exist?
int | $contextid | context ID |
string | $component | component |
string | $filearea | file area |
int | $itemid | item ID |
string | $filepath | file path |
string | $filename | file name |
bool |
file_storage::file_exists_by_hash | ( | $pathnamehash | ) |
Whether or not the file exist.
string | $pathnamehash | path name hash |
bool |
file_storage::get_area_files | ( | $contextid, | |
$component, | |||
$filearea, | |||
$itemid = false, | |||
$sort = "itemid, filepath, filename", | |||
$includedirs = true, | |||
$updatedsince = 0, | |||
$limitfrom = 0, | |||
$limitnum = 0 ) |
Returns all area files (optionally limited by itemid)
int | $contextid | context ID |
string | $component | component |
mixed | $filearea | file area/s, you cannot specify multiple fileareas as well as an itemid |
int | int[] | false | $itemid | item ID(s) or all files if not specified |
string | $sort | A fragment of SQL to use for sorting |
bool | $includedirs | whether or not include directories |
int | $updatedsince | return files updated since this time |
int | $limitfrom | return a subset of records, starting at this point (optional). |
int | $limitnum | return a subset comprising this many records in total (optional, required if $limitfrom is set). |
stored_file[] | array of stored_files indexed by pathanmehash |
file_storage::get_area_tree | ( | $contextid, | |
$component, | |||
$filearea, | |||
$itemid ) |
Returns array based tree structure of area files.
int | $contextid | context ID |
string | $component | component |
string | $filearea | file area |
int | $itemid | item ID |
array | each dir represented by dirname, subdirs, files and dirfile array elements |
file_storage::get_converted_document | ( | stored_file | $file, |
$format, | |||
$forcerefresh = false ) |
Get converted document.
Get an alternate version of the specified document, if it is possible to convert.
stored_file | $file | the file we want to preview |
string | $format | The desired format - e.g. 'pdf'. Formats are specified by file extension. |
boolean | $forcerefresh | If true, the file will be converted every time (not cached). |
stored_file|bool | false if unable to create the conversion, stored file otherwise |
file_storage::get_directory_files | ( | $contextid, | |
$component, | |||
$filearea, | |||
$itemid, | |||
$filepath, | |||
$recursive = false, | |||
$includedirs = true, | |||
$sort = "filepath, filename" ) |
Returns all files and optionally directories.
int | $contextid | context ID |
string | $component | component |
string | $filearea | file area |
int | $itemid | item ID |
int | $filepath | directory path |
bool | $recursive | include all subdirectories |
bool | $includedirs | include files and directories |
string | $sort | A fragment of SQL to use for sorting |
array | of stored_files indexed by pathanmehash |
file_storage::get_external_files | ( | $repositoryid, | |
$sort = '' ) |
Returns all files belonging to given repository.
int | $repositoryid | |
string | $sort | A fragment of SQL to use for sorting |
file_storage::get_file | ( | $contextid, | |
$component, | |||
$filearea, | |||
$itemid, | |||
$filepath, | |||
$filename ) |
Fetch locally stored file.
int | $contextid | context ID |
string | $component | component |
string | $filearea | file area |
int | $itemid | item ID |
string | $filepath | file path |
string | $filename | file name |
stored_file|bool | stored_file instance if exists, false if not |
file_storage::get_file_by_hash | ( | $pathnamehash | ) |
Fetch file using local file full pathname hash.
string | $pathnamehash | path name hash |
stored_file|bool | stored_file instance if exists, false if not |
file_storage::get_file_by_id | ( | $fileid | ) |
Fetch file using local file id.
Please do not rely on file ids, it is usually easier to use pathname hashes instead.
int | $fileid | file ID |
stored_file|bool | stored_file instance if exists, false if not |
file_storage::get_file_instance | ( | stdClass | $filerecord | ) |
Create instance of file class from database record.
stdClass | $filerecord | record from the files table left join files_reference table |
stored_file | instance of file abstraction class |
file_storage::get_file_preview | ( | stored_file | $file, |
$mode ) |
Returns an image file that represent the given stored file as a preview.
At the moment, only GIF, JPEG, PNG and SVG files are supported to have previews. In the future, the support for other mimetypes can be added, too (eg. generate an image preview of PDF, text documents etc).
stored_file | $file | the file we want to preview |
string | $mode | preview mode, eg. 'thumb' |
stored_file|bool | false if unable to create the preview, stored file otherwise |
file_storage::get_file_system | ( | ) |
Return the file system instance.
file_system |
|
static |
Calculates sha1 hash of unique full path name information.
This hash is a unique file identifier - it is used to improve performance and overcome db index size limits.
int | $contextid | context ID |
string | $component | component |
string | $filearea | file area |
int | $itemid | item ID |
string | $filepath | file path |
string | $filename | file name |
string | sha1 hash |
file_storage::get_references_by_storedfile | ( | stored_file | $storedfile | ) |
Returns all aliases that link to the given stored_file.
Aliases in user draft areas are excluded from the returned list.
stored_file | $storedfile |
array | of stored_file |
file_storage::get_references_count_by_storedfile | ( | stored_file | $storedfile | ) |
Returns the number of aliases that link to the given stored_file.
Aliases in user draft areas are not counted.
stored_file | $storedfile |
int |
file_storage::get_unused_dirname | ( | $contextid, | |
$component, | |||
$filearea, | |||
$itemid, | |||
$suggestedpath ) |
Return an available directory name.
This will return the next available directory name in the area, adding/incrementing a suffix of the last portion of path, ie: /path/ > /path (1)/ > /path (2)/ > etc...
If the file path passed is available without modification, it is returned as is.
int | $contextid | context ID. |
string | $component | component. |
string | $filearea | file area. |
int | $itemid | area item ID. |
string | $suggestedpath | the suggested file path. |
string | available file path |
file_storage::get_unused_filename | ( | $contextid, | |
$component, | |||
$filearea, | |||
$itemid, | |||
$filepath, | |||
$filename ) |
Return an available file name.
This will return the next available file name in the area, adding/incrementing a suffix of the file, ie: file.txt > file (1).txt > file (2).txt > etc...
If the file name passed is available without modification, it is returned as is.
int | $contextid | context ID. |
string | $component | component. |
string | $filearea | file area. |
int | $itemid | area item ID. |
string | $filepath | the file path. |
string | $filename | the file name. |
string | available file name. |
coding_exception | if the file name is invalid. |
file_storage::get_user_draft_items | ( | int | $userid, |
int | $updatedsince = 0, | ||
int | $lastnum = 0 ) |
Returns the file area item ids and their updatetime for a user's draft uploads, sorted by updatetime DESC.
int | $userid | user id |
int | $updatedsince | only return draft areas updated since this time |
int | $lastnum | only return the last specified numbers |
array |
|
static |
Calculate and return the contenthash of the supplied file.
string | $filepath | The path to the file on disk |
string | The file's content hash |
|
static |
Calculate and return the contenthash of the supplied content.
string | $content | The file content |
string | The file's content hash |
file_storage::import_external_file | ( | stored_file | $storedfile, |
$maxbytes = 0 ) |
Convert file alias to local file.
moodle_exception | if file could not be downloaded |
stored_file | $storedfile | a stored_file instances |
int | $maxbytes | throw an exception if file size is bigger than $maxbytes (0 means no limit) |
stored_file | stored_file |
file_storage::is_area_empty | ( | $contextid, | |
$component, | |||
$filearea, | |||
$itemid = false, | |||
$ignoredirs = true ) |
Are there any files (or directories)
int | $contextid | context ID |
string | $component | component |
string | $filearea | file area |
bool | int | $itemid | item id or false if all items |
bool | $ignoredirs | whether or not ignore directories |
bool | empty |
|
protected |
Verify the format is supported.
string | $format | The desired format - e.g. 'pdf'. Formats are specified by file extension. |
bool | - True if the format is supported for input. |
|
static |
Return mimetype by given file pathname.
If file has a known extension, we return the mimetype based on extension. Otherwise (when possible) we try to get the mimetype from file contents.
string | $fullpath | Full path to the file on disk |
string | $filename | Correct file name with extension, if omitted will be taken from $path |
string |
|
static |
Inspect a file on disk for it's mimetype.
string | $fullpath | Path to file on disk |
string | The mimetype |
file_storage::move_area_files_to_new_context | ( | $oldcontextid, | |
$newcontextid, | |||
$component, | |||
$filearea, | |||
$itemid = false ) |
Move all the files in a file area from one context to another.
int | $oldcontextid | the context the files are being moved from. |
int | $newcontextid | the context the files are being moved to. |
string | $component | the plugin that these files belong to. |
string | $filearea | the name of the file area. |
int | $itemid | file item ID |
int | the number of files moved, for information. |
|
static |
When user referring to a moodle file, we build the reference field.
array | stdClass | $params |
string |
file_storage::search_references | ( | $reference | ) |
Returns all aliases that refer to some stored_file via the given reference.
All repositories that provide access to a stored_file are expected to use self::pack_reference(). This method can't be used if the given reference does not use this format or if you are looking for references to an external file (for example it can't be used to search for all aliases that refer to a given Dropbox or Box.net file).
Aliases in user draft areas are excluded from the returned list.
string | $reference | identification of the referenced file |
array | of stored_file indexed by its pathnamehash |
file_storage::search_references_count | ( | $reference | ) |
Returns the number of aliases that refer to some stored_file via the given reference.
All repositories that provide access to a stored_file are expected to use self::pack_reference(). This method can't be used if the given reference does not use this format or if you are looking for references to an external file (for example it can't be used to count aliases that refer to a given Dropbox or Box.net file).
Aliases in user draft areas are not counted.
string | $reference | identification of the referenced file |
int |
file_storage::search_server_files | ( | $query, | |
$from = 0, | |||
$limit = 20, | |||
$count = false ) |
Search through the server files.
The query parameter will be used in conjuction with the SQL directive LIKE, so include '' in it if you need to. This search will always ignore user files and directories. Note that the search is case insensitive.
This query can quickly become inefficient so use it sparignly.
string | $query | The string used with SQL LIKE. |
integer | $from | The offset to start the search at. |
integer | $limit | The maximum number of results. |
boolean | $count | When true this methods returns the number of results availabe, disregarding the parameters $from and $limit. |
int|array | Integer when count, otherwise array of stored_file objects. |
file_storage::setup_file_system | ( | ) |
Complete setup procedure for the file_system component.
file_system |
|
protected |
Sorts the result of file_storage::get_area_tree().
array | $tree | Array of results provided by file_storage::get_area_tree() |
array | of sorted results |
file_storage::supports_xsendfile | ( | ) |
Returns true if filesystem is configured to support xsendfile.
bool |
file_storage::synchronise_stored_file_from_file | ( | stored_file | $file, |
$path, | |||
$filerecord ) |
Synchronise stored file from file.
stored_file | $file | Stored file to synchronise. |
string | $path | Path to the file to synchronise from. |
stdClass | $filerecord | The file record from the database. |
file_storage::synchronise_stored_file_from_string | ( | stored_file | $file, |
$content, | |||
$filerecord ) |
Synchronise stored file from string.
stored_file | $file | Stored file to synchronise. |
string | $content | File content. |
stdClass | $filerecord | The file record from the database. |
|
static |
Check if unoconv configured path is correct and working.
stdClass | an object with the test status and the UNOCONVPATH_ constant message. |
file_storage::try_content_recovery | ( | $file | ) |
Tries to recover missing content of file from trash.
stored_file | $file | stored_file instance |
bool | success |
|
static |
Unpack reference field.
string | $str | |
bool | $cleanparams | if set to true, array elements will be passed through clean_param() |
file_reference_exception | if the $str does not have the expected format |
array |
file_storage::update_references | ( | $referencefileid, | |
$lastsync, | |||
$lifetime, | |||
$contenthash, | |||
$filesize, | |||
$status, | |||
$timemodified = null ) |
Updates a reference to the external resource and all files that use it.
This function is called after synchronisation of an external file and updates the contenthash, filesize and status of all files that reference this external file as well as time last synchronised.
int | $referencefileid | |
int | $lastsync | |
int | $lifetime | argument not used any more, liefetime is returned by repository |
string | $contenthash | |
int | $filesize | |
int | $status | 0 if ok or 666 if source is missing |
int | $timemodified | last time modified of the source, if known |
file_storage::update_references_to_storedfile | ( | stored_file | $storedfile | ) |
Updates all files that are referencing this file with the new contenthash and filesize.
stored_file | $storedfile |
file_storage::xsendfile | ( | $contenthash | ) |
Serve file content using X-Sendfile header.
Please make sure that all headers are already sent and the all access control checks passed.
string | $contenthash | sah1 hash of the file content to be served |
bool | success |
file_storage::xsendfile_file | ( | stored_file | $file | ) |
Serve file content using X-Sendfile header.
Please make sure that all headers are already sent and the all access control checks passed.
This alternate method to xsendfile() allows an alternate file system to use the full file metadata and avoid extra lookups.
stored_file | $file | The file to send |
bool | success |