Moodle APIs  3.8
Moodle 3.8.6 (Build: 20201109)
Public Member Functions | Protected Member Functions | Static Protected Member Functions | List of all members
file_system Class Reference
Inheritance diagram for file_system:
file_system_filedir

Public Member Functions

 add_file_from_path ($pathname, $contenthash=null)
 Add the supplied file to the file system. More...
 
 add_file_from_string ($content)
 Add a file with the supplied content to the file system. More...
 
 add_storedfile_to_archive (stored_file $file, file_archive $filearch, $archivepath)
 Add file/directory into archive. More...
 
 add_to_curl_request (stored_file $file, &$curlrequest, $key)
 Adds this file path to a curl request (POST only). More...
 
 copy_content_from_storedfile (stored_file $file, $target)
 Copy content of file to given pathname. More...
 
 cron ()
 Run any periodic tasks which must be performed.
 
 extract_to_pathname (stored_file $file, file_packer $packer, $pathname, file_progress $progress=null)
 Extract file to given file path (real OS filesystem), existing files are overwritten. More...
 
 extract_to_storage (stored_file $file, file_packer $packer, $contextid, $component, $filearea, $itemid, $pathbase, $userid=null, file_progress $progress=null)
 Extract file to given file path (real OS filesystem), existing files are overwritten. More...
 
 get_content (stored_file $file)
 Get the content of the specified stored file. More...
 
 get_content_file_handle (stored_file $file, $type=stored_file::FILE_HANDLE_FOPEN)
 Returns file handle - read only mode, no writing allowed into pool files! More...
 
 get_imageinfo (stored_file $file)
 Returns information about image. More...
 
 get_local_path_from_storedfile (stored_file $file, $fetchifnotfound=false)
 Get the full path on disk for the specified stored file. More...
 
 get_remote_path_from_storedfile (stored_file $file)
 Get a remote filepath for the specified stored file. More...
 
 is_file_readable_locally_by_hash ($contenthash, $fetchifnotfound=false)
 Determine whether the file is present on the file system somewhere given the contenthash. More...
 
 is_file_readable_locally_by_storedfile (stored_file $file, $fetchifnotfound=false)
 Determine whether the file is present on the file system somewhere. More...
 
 is_file_readable_remotely_by_hash ($contenthash)
 Determine whether the file is present locally on the file system somewhere given the contenthash. More...
 
 is_file_readable_remotely_by_storedfile (stored_file $file)
 Determine whether the file is present on the local file system somewhere. More...
 
 is_image_from_storedfile (stored_file $file)
 Attempt to determine whether the specified file is likely to be an image. More...
 
 list_files ($file, file_packer $packer)
 List contents of archive. More...
 
 mimetype_from_hash ($contenthash, $filename)
 Retrieve the mime information for the specified stored file. More...
 
 mimetype_from_storedfile ($file)
 Retrieve the mime information for the specified stored file. More...
 
 readfile (stored_file $file)
 Output the content of the specified stored file. More...
 
 remove_file ($contenthash)
 Remove the file with the specified contenthash. More...
 
 supports_xsendfile ()
 Returns true if filesystem is configured to support xsendfile. More...
 
 xsendfile ($contenthash)
 Serve file content using X-Sendfile header. More...
 

Protected Member Functions

 __clone ()
 Private clone method to prevent cloning of the instance.
 
 __wakeup ()
 Private wakeup method to prevent unserialising of the instance.
 
 get_imageinfo_from_path ($path)
 Returns image information relating to the specified path or URL. More...
 
 get_local_path_from_hash ($contenthash, $fetchifnotfound=false)
 Get the full path for the specified hash, including the path to the filedir. More...
 
 get_remote_path_from_hash ($contenthash)
 Get the full path for the specified hash, including the path to the filedir. More...
 
 validate_hash_and_file_size ($contenthash, $pathname)
 Validate that the content hash matches the content hash of the file on disk. More...
 

Static Protected Member Functions

static get_file_handle_for_path ($path, $type=stored_file::FILE_HANDLE_FOPEN)
 Return a file handle for the specified path. More...
 
static is_file_removable ($contenthash)
 Check whether a file is removable. More...
 

Member Function Documentation

◆ add_file_from_path()

file_system::add_file_from_path (   $pathname,
  $contenthash = null 
)
abstract

Add the supplied file to the file system.

Note: If overriding this function, it is advisable to store the file in the path returned by get_local_path_from_hash as there may be subsequent uses of the file in the same request.

Parameters
string$pathnamePath to file currently on disk
string$contenthashSHA1 hash of content if known (performance only)
Return values
array(contenthash, filesize, newfile)

Reimplemented in file_system_filedir.

◆ add_file_from_string()

file_system::add_file_from_string (   $content)
abstract

Add a file with the supplied content to the file system.

Note: If overriding this function, it is advisable to store the file in the path returned by get_local_path_from_hash as there may be subsequent uses of the file in the same request.

Parameters
string$contentfile content - binary string
Return values
array(contenthash, filesize, newfile)

Reimplemented in file_system_filedir.

◆ add_storedfile_to_archive()

file_system::add_storedfile_to_archive ( stored_file  $file,
file_archive  $filearch,
  $archivepath 
)

Add file/directory into archive.

Parameters
stored_file$fileThe file to archive
file_archive$filearchfile archive instance
string$archivepathpathname in archive
Return values
boolsuccess

◆ add_to_curl_request()

file_system::add_to_curl_request ( stored_file  $file,
$curlrequest,
  $key 
)

Adds this file path to a curl request (POST only).

Parameters
stored_file$fileThe file to add to the curl request
curl$curlrequestThe curl request object
string$keyWhat key to use in the POST request
Return values
voidThis needs the fullpath for the storedfile :/ Can this be achieved in some other fashion?

◆ copy_content_from_storedfile()

file_system::copy_content_from_storedfile ( stored_file  $file,
  $target 
)
abstract

Copy content of file to given pathname.

Parameters
stored_file$fileThe file to be copied
string$targetreal path to the new file
Return values
boolsuccess

Reimplemented in file_system_filedir.

◆ extract_to_pathname()

file_system::extract_to_pathname ( stored_file  $file,
file_packer  $packer,
  $pathname,
file_progress  $progress = null 
)

Extract file to given file path (real OS filesystem), existing files are overwritten.

Parameters
stored_file$fileThe archive to inspect
file_packer$packerFile packer instance
string$pathnameTarget directory
file_progress$progressprogress indicator callback or null if not required
Return values
array|boolList of processed files; false if error

◆ extract_to_storage()

file_system::extract_to_storage ( stored_file  $file,
file_packer  $packer,
  $contextid,
  $component,
  $filearea,
  $itemid,
  $pathbase,
  $userid = null,
file_progress  $progress = null 
)

Extract file to given file path (real OS filesystem), existing files are overwritten.

Parameters
stored_file$fileThe archive to inspect
file_packer$packerfile packer instance
int$contextidcontext ID
string$componentcomponent
string$fileareafile area
int$itemiditem ID
string$pathbasepath base
int$useriduser ID
file_progress$progressProgress indicator callback or null if not required
Return values
array|boollist of processed files; false if error

◆ get_content()

file_system::get_content ( stored_file  $file)

Get the content of the specified stored file.

Generally you will probably want to use readfile() to serve content, and where possible you should see if you can use get_content_file_handle and work with the file stream instead.

Parameters
stored_file$fileThe file to retrieve
Return values
stringThe full file content

◆ get_content_file_handle()

file_system::get_content_file_handle ( stored_file  $file,
  $type = stored_file::FILE_HANDLE_FOPEN 
)

Returns file handle - read only mode, no writing allowed into pool files!

When you want to modify a file, create a new file and delete the old one.

Parameters
stored_file$fileThe file to retrieve a handle for
int$typeType of file handle (FILE_HANDLE_xx constant)
Return values
resourcefile handle

◆ get_file_handle_for_path()

static file_system::get_file_handle_for_path (   $path,
  $type = stored_file::FILE_HANDLE_FOPEN 
)
staticprotected

Return a file handle for the specified path.

This abstraction should be used when overriding get_content_file_handle in a new file system.

Parameters
string$pathThe path to the file. This shoudl be any type of path that fopen and gzopen accept.
int$typeType of file handle (FILE_HANDLE_xx constant)
Return values
resource
Exceptions
coding_exceptionWhen an unexpected type of file handle is requested

◆ get_imageinfo()

file_system::get_imageinfo ( stored_file  $file)

Returns information about image.

Information is determined from the file content

Parameters
stored_file$fileThe file to inspect
Return values
mixedarray with width, height and mimetype; false if not an image

◆ get_imageinfo_from_path()

file_system::get_imageinfo_from_path (   $path)
protected

Returns image information relating to the specified path or URL.

Parameters
string$pathThe path to pass to getimagesize.
Return values
arrayContaining width, height, and mimetype.

◆ get_local_path_from_hash()

file_system::get_local_path_from_hash (   $contenthash,
  $fetchifnotfound = false 
)
abstractprotected

Get the full path for the specified hash, including the path to the filedir.

Note: This must return a consistent path for the file's contenthash and the path will be in a standard local format. Streamable paths will not work. A local copy of the file will be fetched if $fetchifnotfound is tree.

The $fetchifnotfound allows you to determine the expected path of the file.

Parameters
string$contenthashThe content hash
bool$fetchifnotfoundWhether to attempt to fetch from the remote path if not found.
Return values
stringThe full path to the content file

Reimplemented in file_system_filedir.

◆ get_local_path_from_storedfile()

file_system::get_local_path_from_storedfile ( stored_file  $file,
  $fetchifnotfound = false 
)

Get the full path on disk for the specified stored file.

Note: This must return a consistent path for the file's contenthash and the path will be in a standard local format. Streamable paths will not work. A local copy of the file will be fetched if $fetchifnotfound is tree.

The $fetchifnotfound allows you to determine the expected path of the file.

Parameters
stored_file$fileThe file to serve.
bool$fetchifnotfoundWhether to attempt to fetch from the remote path if not found.
Return values
stringfull path to pool file with file content

Reimplemented in file_system_filedir.

◆ get_remote_path_from_hash()

file_system::get_remote_path_from_hash (   $contenthash)
abstractprotected

Get the full path for the specified hash, including the path to the filedir.

This is typically either the same as the local filepath, or it is a streamable resource.

See https://secure.php.net/manual/en/wrappers.php for further information on valid wrappers.

Parameters
string$contenthashThe content hash
Return values
stringThe full path to the content file

Reimplemented in file_system_filedir.

◆ get_remote_path_from_storedfile()

file_system::get_remote_path_from_storedfile ( stored_file  $file)

Get a remote filepath for the specified stored file.

This is typically either the same as the local filepath, or it is a streamable resource.

See https://secure.php.net/manual/en/wrappers.php for further information on valid wrappers.

Parameters
stored_file$fileThe file to serve.
Return values
stringfull path to pool file with file content

Reimplemented in file_system_filedir.

◆ is_file_readable_locally_by_hash()

file_system::is_file_readable_locally_by_hash (   $contenthash,
  $fetchifnotfound = false 
)

Determine whether the file is present on the file system somewhere given the contenthash.

Parameters
string$contenthashThe contenthash of the file to check.
bool$fetchifnotfoundWhether to attempt to fetch from the remote path if not found.
Return values
bool

◆ is_file_readable_locally_by_storedfile()

file_system::is_file_readable_locally_by_storedfile ( stored_file  $file,
  $fetchifnotfound = false 
)

Determine whether the file is present on the file system somewhere.

A local copy of the file will be fetched if $fetchifnotfound is tree.

The $fetchifnotfound allows you to determine the expected path of the file.

Parameters
stored_file$fileThe file to ensure is available.
bool$fetchifnotfoundWhether to attempt to fetch from the remote path if not found.
Return values
bool

◆ is_file_readable_remotely_by_hash()

file_system::is_file_readable_remotely_by_hash (   $contenthash)

Determine whether the file is present locally on the file system somewhere given the contenthash.

Parameters
string$contenthashThe contenthash of the file to check.
Return values
bool

◆ is_file_readable_remotely_by_storedfile()

file_system::is_file_readable_remotely_by_storedfile ( stored_file  $file)

Determine whether the file is present on the local file system somewhere.

Parameters
stored_file$fileThe file to ensure is available.
Return values
bool

◆ is_file_removable()

static file_system::is_file_removable (   $contenthash)
staticprotected

Check whether a file is removable.

This must be called prior to file removal.

Parameters
string$contenthash
Return values
bool

◆ is_image_from_storedfile()

file_system::is_image_from_storedfile ( stored_file  $file)

Attempt to determine whether the specified file is likely to be an image.

Since this relies upon the mimetype stored in the files table, there may be times when this information is not 100% accurate.

Parameters
stored_file$fileThe file to check
Return values
bool

◆ list_files()

file_system::list_files (   $file,
file_packer  $packer 
)

List contents of archive.

Parameters
stored_file$fileThe archive to inspect
file_packer$packerfile packer instance
Return values
arrayof file infos

◆ mimetype_from_hash()

file_system::mimetype_from_hash (   $contenthash,
  $filename 
)

Retrieve the mime information for the specified stored file.

Parameters
string$contenthash
string$filename
Return values
stringThe MIME type.

◆ mimetype_from_storedfile()

file_system::mimetype_from_storedfile (   $file)

Retrieve the mime information for the specified stored file.

Parameters
stored_file$fileThe stored file to retrieve mime information for
Return values
stringThe MIME type.

◆ readfile()

file_system::readfile ( stored_file  $file)

Output the content of the specified stored file.

Note, this is different to get_content() as it uses the built-in php readfile function which is more efficient.

Parameters
stored_file$fileThe file to serve.
Return values
void

◆ remove_file()

file_system::remove_file (   $contenthash)
abstract

Remove the file with the specified contenthash.

Note, if overriding this function, you must check that the file is no longer in use - see {check_file_usage}.

DO NOT call directly - reserved for core!!

Parameters
string$contenthash

Reimplemented in file_system_filedir.

◆ supports_xsendfile()

file_system::supports_xsendfile ( )

Returns true if filesystem is configured to support xsendfile.

Return values
bool

◆ validate_hash_and_file_size()

file_system::validate_hash_and_file_size (   $contenthash,
  $pathname 
)
protected

Validate that the content hash matches the content hash of the file on disk.

Parameters
string$contenthashThe current content hash to validate
string$pathnameThe path to the file on disk
Return values
arrayThe content hash (it might change) and file size

◆ xsendfile()

file_system::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.

Parameters
string$contenthashThe content hash of the file to be served
Return values
boolsuccess

The documentation for this class was generated from the following file: