# Class yii\mongodb\file\StreamWrapper

Inheritance | yii\mongodb\file\StreamWrapper » yii\base\BaseObject
Implements | yii\base\Configurable
Available since version | 2.1
Source Code | https://github.com/yiisoft/yii2-mongodb/blob/master/file/StreamWrapper.php

StreamWrapper provides stream wrapper for MongoDB GridFS, allowing file operations via regular PHP stream resources.

Before feature can be used this wrapper should be registered via register() method. It is usually performed via yii\mongodb\Connection::registerFileStreamWrapper().

Note: do not use this class directly - its instance will be created and maintained by PHP internally once corresponding stream resource is created.

Resource path should be specified in following format:

    'protocol://databaseName.fileCollectionPrefix?file_attribute=value'

Write example:

    $resource = fopen('gridfs://mydatabase.fs?filename=new_file.txt', 'w');
    fwrite($resource, 'some content');
    // ...
    fclose($resource);

Read example:

    $resource = fopen('gridfs://mydatabase.fs?filename=my_file.txt', 'r');
    $fileContent = stream_get_contents($resource);

See also http://php.net/manual/en/function.stream-wrapper-register.php.

## Public Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$context | resource | Associated stream resource context. | yii\mongodb\file\StreamWrapper
$contextOptions | array | Context options. This property is read-only. | yii\mongodb\file\StreamWrapper

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
__call() | Calls the named method which is not a class method. | yii\base\BaseObject
__construct() | Constructor. | yii\base\BaseObject
__get() | Returns the value of an object property. | yii\base\BaseObject
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject
__set() | Sets value of an object property. | yii\base\BaseObject
__unset() | Sets an object property to null. | yii\base\BaseObject
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\BaseObject
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\BaseObject
className() | Returns the fully qualified name of this class. | yii\base\BaseObject
getContextOptions() | Returns options associated with $context. | yii\mongodb\file\StreamWrapper
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\BaseObject
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\BaseObject
init() | Initializes the object. | yii\base\BaseObject
register() | Registers this steam wrapper. | yii\mongodb\file\StreamWrapper
stream_close() | Closes a resource. | yii\mongodb\file\StreamWrapper
stream_eof() | Tests for end-of-file on a file pointer. | yii\mongodb\file\StreamWrapper
stream_flush() | This method is called in response to `fflush()` and when the stream is being closed while any unflushed data has been written to it before. | yii\mongodb\file\StreamWrapper
stream_open() | Opens file. | yii\mongodb\file\StreamWrapper
stream_read() | Reads from stream. | yii\mongodb\file\StreamWrapper
stream_seek() | Seeks to specific location in a stream. | yii\mongodb\file\StreamWrapper
stream_stat() | Retrieve information about a file resource. | yii\mongodb\file\StreamWrapper
stream_tell() | Retrieve the current position of a stream. | yii\mongodb\file\StreamWrapper
stream_write() | Writes to stream. | yii\mongodb\file\StreamWrapper

## Property Details

$context public property

Associated stream resource context. This property is set automatically by PHP once wrapper is instantiated.

public resource $context = null
$contextOptions public property

Context options. This property is read-only.

public array $contextOptions = null

## Method Details

getContextOptions() public method

Returns options associated with $context.

public array getContextOptions ( )
return | array | Context options.

register() public static method

Registers this steam wrapper.

public static void register ( $protocol = 'gridfs', $force = false )
$protocol | string | Name of the protocol to be used.
$force | boolean | Whether to register wrapper, even if protocol is already taken.

stream_close() public method

Closes a resource.

This method is called in response to `fclose()`.

See also fclose().

public void stream_close ( )

stream_eof() public method

Tests for end-of-file on a file pointer.

This method is called in response to `feof()`.

See also feof().

public boolean stream_eof ( )
return | boolean | `true` if the read/write position is at the end of the stream and if no more data is available to be read, or `false` otherwise.

stream_flush() public method

This method is called in response to `fflush()` and when the stream is being closed while any unflushed data has been written to it before.

See also fflush().

public boolean stream_flush ( )
return | boolean | Whether cached data was successfully stored.

stream_open() public method

Opens file.

This method is called immediately after the wrapper is initialized (f.e. by `fopen()` and `file_get_contents()`).

See also fopen().

public boolean stream_open ( $path, $mode, $options, &$openedPath )
$path | string | Specifies the URL that was passed to the original function.
$mode | string | Mode used to open the file, as detailed for `fopen()`.
$options | integer | Additional flags set by the streams API.
$openedPath | string | Real opened path.
return | boolean | Whether operation is successful.

stream_read() public method

Reads from stream.

This method is called in response to `fread()` and `fgets()`.

See also fread().

public string|false stream_read ( $count )
$count | integer | Count of bytes of data from the current position should be returned.
return | string|false | If there are less than count bytes available, return as many as are available. If no more data is available, return `false`.

stream_seek() public method

Seeks to specific location in a stream.

This method is called in response to `fseek()`.

See also fseek().

public boolean stream_seek ( $offset, $whence = SEEK_SET )
$offset | integer | The stream offset to seek to.
$whence | integer | Possible values: SEEK_SET - Set position equal to offset bytes. SEEK_CUR - Set position to current location plus offset. SEEK_END - Set position to end-of-file plus offset.
return | boolean | Return true if the position was updated, false otherwise.

stream_stat() public method

Retrieve information about a file resource.

This method is called in response to `stat()`.

See also stat().

public array stream_stat ( )
return | array | File statistic information.

stream_tell() public method

Retrieve the current position of a stream.

This method is called in response to `fseek()` to determine the current position.

See also fseek().

public integer stream_tell ( )
return | integer | Should return the current position of the stream.

stream_write() public method

Writes to stream.

This method is called in response to `fwrite()`.

See also fwrite().

public integer stream_write ( $data )
$data | string | String to be stored into the underlying stream.
return | integer | The number of bytes that were successfully stored.