FileSystem
class FileSystem implements FileSystemInterface (View source)
Provides helpers to operate on files and stream wrappers.
Constants
| CHMOD_DIRECTORY |
Default mode for new directories. See self::chmod(). |
| CHMOD_FILE |
Default mode for new files. See self::chmod(). |
Properties
| protected Settings | $settings | The site settings. |
|
| protected LoggerInterface | $logger | The file logger channel. |
|
| protected StreamWrapperManagerInterface | $streamWrapperManager | The stream wrapper manager. |
Methods
Constructs a new FileSystem.
Moves an uploaded file to a new location.
Sets the permissions on a file or directory.
Deletes a file.
Resolves the absolute filepath of a local URI or filepath.
Gets the name of the directory from a given path.
Gets the filename from a given path.
Creates a directory, optionally creating missing components in the path to the directory.
Helper function. Ensures we don't pass a NULL as a context resource to mkdir().
Removes a directory.
Creates a file with a unique filename in the specified directory.
Copies a file to a new location without invoking the file API.
Deletes a file without database changes or hook invocations.
Deletes all files and directories in the specified filepath recursively.
Moves a file to a new location without database changes or hook invocation.
Prepares the destination for a file copy or move operation.
Saves a file to the specified destination without invoking file API.
Checks that the directory exists and is writable.
Determines the destination path for a file.
Creates a full file path from a directory and filename.
Gets the path of the configured temporary directory.
Finds all files that match a given mask in a given directory.
Internal function to handle directory scanning with recursion.
Details
__construct(StreamWrapperManagerInterface $stream_wrapper_manager, Settings $settings, LoggerInterface $logger)
Constructs a new FileSystem.
bool
moveUploadedFile(string $filename, string $uri)
Moves an uploaded file to a new location.
PHP's move_uploaded_file() does not properly support streams if open_basedir is enabled, so this function fills that gap.
Compatibility: normal paths and stream wrappers.
bool
chmod(string $uri, int $mode = NULL)
Sets the permissions on a file or directory.
This function will use the file_chmod_directory and file_chmod_file settings for the default modes for directories and uploaded/generated files. By default these will give everyone read access so that users accessing the files with a user account without the webserver group (e.g. via FTP) can read these files, and give group write permissions so webserver group members (e.g. a vhost account) can alter files uploaded and owned by the webserver.
PHP's chmod does not support stream wrappers so we use our wrapper implementation which interfaces with chmod() by default. Contrib wrappers may override this behavior in their implementations as needed.
bool
unlink(string $uri, resource $context = NULL)
Deletes a file.
PHP's unlink() is broken on Windows, as it can fail to remove a file when it has a read-only flag set.
string|false
realpath(string $uri)
Resolves the absolute filepath of a local URI or filepath.
The use of this method is discouraged, because it does not work for remote URIs. Except in rare cases, URIs should not be manually resolved.
Only use this function if you know that the stream wrapper in the URI uses the local file system, and you need to pass an absolute path to a function that is incompatible with stream URIs.
string
dirname(string $uri)
Gets the name of the directory from a given path.
PHP's dirname() does not properly pass streams, so this function fills that gap. It is backwards compatible with normal paths and will use PHP's dirname() as a fallback.
Compatibility: normal paths and stream wrappers.
basename($uri, $suffix = NULL)
Gets the filename from a given path.
PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character.
bool
mkdir(string $uri, int $mode = NULL, bool $recursive = FALSE, resource $context = NULL)
Creates a directory, optionally creating missing components in the path to the directory.
When PHP's mkdir() creates a directory, the requested mode is affected by the process's umask. This function overrides the umask and sets the mode explicitly for all directory components created.
protected
mkdirCall($uri, $mode, $recursive, $context)
Helper function. Ensures we don't pass a NULL as a context resource to mkdir().
bool
rmdir(string $uri, resource $context = NULL)
Removes a directory.
PHP's rmdir() is broken on Windows, as it can fail to remove a directory when it has a read-only flag set.
string|bool
tempnam(string $directory, string $prefix)
Creates a file with a unique filename in the specified directory.
PHP's tempnam() does not return a URI like we want. This function will return a URI if given a URI, or it will return a filepath if given a filepath.
Compatibility: normal paths and stream wrappers.
string
copy(string $source, string $destination, int $replace = self::EXISTS_RENAME)
Copies a file to a new location without invoking the file API.
This is a powerful function that in many ways performs like an advanced version of copy().
- Checks if $source and $destination are valid and readable/writable.
- If file already exists in $destination either the call will error out, replace the file or rename the file based on the $replace parameter.
- If the $source and $destination are equal, the behavior depends on the $replace parameter. FileSystemInterface::EXISTS_REPLACE will replace the existing file. FileSystemInterface::EXISTS_ERROR will error out. FileSystemInterface::EXISTS_RENAME will rename the file until the $destination is unique.
- Provides a fallback using realpaths if the move fails using stream wrappers. This can occur because PHP's copy() function does not properly support streams if open_basedir is enabled. See https://bugs.php.net/bug.php?id=60456
delete(string $path)
Deletes a file without database changes or hook invocations.
This function should be used when the file to be deleted does not have an entry recorded in the files table.
deleteRecursive(string $path, callable $callback = NULL)
Deletes all files and directories in the specified filepath recursively.
If the specified path is a directory then the function is called recursively to process the contents. Once the contents have been removed the directory is also removed.
If the specified path is a file then it will be processed with delete() method.
Note that this only deletes visible files with write permission.
string
move(string $source, string $destination, int $replace = self::EXISTS_RENAME)
Moves a file to a new location without database changes or hook invocation.
This is a powerful function that in many ways performs like an advanced version of rename().
- Checks if $source and $destination are valid and readable/writable.
- Checks that $source is not equal to $destination; if they are an error is reported.
- If file already exists in $destination either the call will error out, replace the file or rename the file based on the $replace parameter.
- Works around a PHP bug where rename() does not properly support streams if safe_mode or open_basedir are enabled.
protected
prepareDestination(string $source, string|null $destination, int $replace)
Prepares the destination for a file copy or move operation.
- Checks if $source and $destination are valid and readable/writable.
- Checks that $source is not equal to $destination; if they are an error is reported.
- If file already exists in $destination either the call will error out, replace the file or rename the file based on the $replace parameter.
string
saveData(string $data, string $destination, int $replace = self::EXISTS_RENAME)
Saves a file to the specified destination without invoking file API.
This function is identical to file_save_data() except the file will not be saved to the {filemanaged} table and none of the file* hooks will be called.
bool
prepareDirectory(string $directory, int $options = self::MODIFY_PERMISSIONS)
Checks that the directory exists and is writable.
Directories need to have execute permissions to be considered a directory by FTP servers, etc.
string|bool
getDestinationFilename(string $destination, int $replace)
Determines the destination path for a file.
string
createFilename(string $basename, string $directory)
Creates a full file path from a directory and filename.
If a file with the specified name already exists, an alternative will be used.
string
getTempDirectory()
Gets the path of the configured temporary directory.
If the path is not set, it will fall back to the OS-specific default if set, otherwise a directory under the public files directory. It will then set this as the configured directory.
array
scanDirectory(string $dir, string $mask, array $options = [])
Finds all files that match a given mask in a given directory.
Directories and files beginning with a dot are excluded; this prevents hidden files and directories (such as SVN working directories) from being scanned. Use the umask option to skip configuration directories to eliminate the possibility of accidentally exposing configuration information. Also, you can use the base directory, recurse, and min_depth options to improve performance by limiting how much of the filesystem has to be traversed.
protected array
doScanDirectory(string $dir, string $mask, array $options = [], int $depth = 0)
Internal function to handle directory scanning with recursion.