LocalStream
abstract class LocalStream implements StreamWrapperInterface (View source)
Defines a Drupal stream wrapper base class for local files.
This class provides a complete stream wrapper implementation. URIs such as "public://example.txt" are expanded to a normal filesystem path such as "sites/default/files/example.txt" and then PHP filesystem functions are invoked.
Drupal\Core\StreamWrapper\LocalStream implementations need to implement at least the getDirectoryPath() and getExternalUrl() methods.
Properties
| resource | $context | Stream context resource. |
|
| resource | $handle | A generic resource handle. |
|
| protected string | $uri | Instance URI (stream). |
Methods
Returns the type of stream wrapper.
Gets the path that the wrapper is responsible for.
Sets the absolute stream resource URI.
Returns the stream resource URI.
Returns the local writable target of the resource within the stream.
Returns canonical, absolute path of the resource.
Returns the canonical absolute path of the URI, if possible.
Opens file or URL.
Advisory file locking.
Read from stream.
Write to stream.
Tests for end-of-file on a file pointer.
Seeks to specific location in a stream.
Flushes the output.
Retrieve the current position of a stream.
Retrieve information about a file resource.
Closes stream.
Retrieve the underlying stream resource.
Sets metadata on the stream.
Change stream options.
Truncate stream.
Delete a file.
Renames a file or directory.
Gets the name of the directory from a given path.
Create a directory.
Removes a directory.
Retrieve information about a file.
Open directory handle.
Read entry from directory handle.
Rewind directory handle.
Close directory handle.
Details
static int
getType()
Returns the type of stream wrapper.
abstract string
getDirectoryPath()
Gets the path that the wrapper is responsible for.
setUri(string $uri)
Sets the absolute stream resource URI.
This allows you to set the URI. Generally is only called by the factory method.
string
getUri()
Returns the stream resource URI.
protected string|bool
getTarget(string $uri = NULL)
Returns the local writable target of the resource within the stream.
This function should be used in place of calls to realpath() or similar functions when attempting to determine the location of a file. While functions like realpath() may return the location of a read-only file, this method may return a URI or path suitable for writing that is completely separate from the URI used for reading.
string
realpath()
Returns canonical, absolute path of the resource.
Implementation placeholder. PHP's realpath() does not support stream wrappers. We provide this as a default so that individual wrappers may implement their own solutions.
protected string|bool
getLocalPath(string $uri = NULL)
Returns the canonical absolute path of the URI, if possible.
bool
stream_open($uri, string $mode, int $options, string $opened_path)
Opens file or URL.
This method is called immediately after the wrapper is initialized (e.g. by fopen() and file_get_contents()).
Note the streamWrapper::$context property is updated if a valid context is passed to the caller function.
bool
stream_lock(int $operation)
Advisory file locking.
This method is called in response to flock(), when file_put_contents() (when flags contains LOCK_EX), stream_set_blocking() and when closing the stream (LOCK_UN).
string|false
stream_read(int $count)
Read from stream.
This method is called in response to fread() and fgets().
Note, remember to update the read/write position of the stream (by the number of bytes that were successfully read).
Note, PhpStreamWrapperInterface::stream_eof() is called directly after calling PhpStreamWrapperInterface::stream_read() to check if EOF has been reached. If not implemented, EOF is assumed.
Warning, when reading the whole file (e.g., with file_get_contents()), PHP will call PhpStreamWrapperInterface::stream_read() followed by PhpStreamWrapperInterface::stream_eof() in a loop but as long as PhpStreamWrapperInterface::stream_read() returns a non-empty string, the return value of PhpStreamWrapperInterface::stream_eof() is ignored.
int
stream_write(string $data)
Write to stream.
This method is called in response to fwrite(). Remember to update the current position of the stream by number of bytes that were successfully written.
bool
stream_eof()
Tests for end-of-file on a file pointer.
This method is called in response to feof().
Warning, when reading the whole file (for example, with file_get_contents()), PHP will call PhpStreamWrapperInterface::stream_read() followed by PhpStreamWrapperInterface::stream_eof() in a loop but as long as PhpStreamWrapperInterface::stream_read() returns a non-empty string, the return value of PhpStreamWrapperInterface::stream_eof() is ignored.
bool
stream_seek(int $offset, int $whence = SEEK_SET)
Seeks to specific location in a stream.
This method is called in response to fseek().
The read/write position of the stream should be updated according to the offset and whence.
bool
stream_flush()
Flushes the output.
This method is called in response to fflush() and when the stream is being closed while any un-flushed data has been written to it before. If you have cached data in your stream but not yet stored it into the underlying storage, you should do so now.
Note, if not implemented, FALSE is assumed as the return value.
int
stream_tell()
Retrieve the current position of a stream.
This method is called in response to fseek() to determine the current position.
array|false
stream_stat()
Retrieve information about a file resource.
This method is called in response to fstat().
stream_close()
Closes stream.
This method is called in response to fclose(). All resources that were locked, or allocated, by the wrapper should be released.
resource|false
stream_cast(int $cast_as)
Retrieve the underlying stream resource.
This method is called in response to stream_select().
bool
stream_metadata($uri, int $option, mixed $value)
Sets metadata on the stream.
bool
stream_set_option(int $option, int $arg1, int $arg2)
Change stream options.
This method is called to set options on the stream.
bool
stream_truncate(int $new_size)
Truncate stream.
Will respond to truncation; e.g., through ftruncate().
bool
unlink($uri)
Delete a file.
This method is called in response to unlink().
Note, in order for the appropriate error message to be returned this method should not be defined if the wrapper does not support removing files.
Note, the streamWrapper::$context property is updated if a valid context is passed to the caller function.
bool
rename($from_uri, $to_uri)
Renames a file or directory.
This method is called in response to rename(). Should attempt to rename $path_from to $path_to.
Note, in order for the appropriate error message to be returned this method should not be defined if the wrapper does not support renaming files.
Note, the streamWrapper::$context property is updated if a valid context is passed to the caller function.
string
dirname(string $uri = NULL)
Gets the name of the directory from a given path.
This method is usually accessed through \Drupal\Core\File\FileSystemInterface::dirname(), which wraps around the normal PHP dirname() function, which does not support stream wrappers.
bool
mkdir($uri, int $mode, int $options)
Create a directory.
This method is called in response to mkdir()
Note, in order for the appropriate error message to be returned this method should not be defined if the wrapper does not support creating directories.
Note, the streamWrapper::$context property is updated if a valid context is passed to the caller function.
bool
rmdir($uri, int $options)
Removes a directory.
This method is called in response to rmdir().
Note, in order for the appropriate error message to be returned this method should not be defined if the wrapper does not support removing directories.
Note, the streamWrapper::$context property is updated if a valid context is passed to the caller function.
array|false
url_stat($uri, int $flags)
Retrieve information about a file.
This method is called in response to all stat() related functions.
Note, the streamWrapper::$context property is updated if a valid context is passed to the caller function.
bool
dir_opendir($uri, int $options)
Open directory handle.
This method is called in response to opendir().
string|false
dir_readdir()
Read entry from directory handle.
This method is called in response to readdir().
bool
dir_rewinddir()
Rewind directory handle.
This method is called in response to rewinddir(). Should reset the output generated by PhpStreamWrapperInterface::dir_readdir. The next call to PhpStreamWrapperInterface::dir_readdir should return the first entry in the location returned by PhpStreamWrapperInterface::dir_opendir.
bool
dir_closedir()
Close directory handle.
This method is called in response to closedir(). Any resources which were locked, or allocated, during opening and use of the directory stream should be released.