# Class yii\mongodb\file\Download

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

Download represents the GridFS download operation.

A `Download` object is usually created by calling yii\mongodb\file\Collection::get() or yii\mongodb\file\Collection::createDownload().

Usage example:

    Yii::$app->mongodb->getFileCollection()->createDownload($document['_id'])->toFile('/path/to/file.dat');

You can use `Download::substr()` to read a specific part of the file:

    $filePart = Yii::$app->mongodb->getFileCollection()->createDownload($document['_id'])->substr(256, 1024);

## Public Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$bytes | string | File content. This property is read-only. | yii\mongodb\file\Download
$chunkCursor | \MongoDB\Driver\Cursor | Chuck list cursor. This property is read-only. | yii\mongodb\file\Download
$chunkIterator | Iterator | Chuck cursor iterator. This property is read-only. | yii\mongodb\file\Download
$collection | yii\mongodb\file\Collection | File collection to be used. | yii\mongodb\file\Download
$document | array | Document to be downloaded. Note that the type of this property differs in getter and setter. See getDocument() and setDocument() for details. | yii\mongodb\file\Download
$filename | string|null | File name. This property is read-only. | yii\mongodb\file\Download
$resource | resource | File stream resource. This property is read-only. | yii\mongodb\file\Download
$size | integer | File size. This property is read-only. | yii\mongodb\file\Download

## 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
getBytes() | Alias of toString() method. | yii\mongodb\file\Download
getChunkCursor() | Returns file chunks read cursor. | yii\mongodb\file\Download
getChunkIterator() | Returns iterator for the file chunks cursor. | yii\mongodb\file\Download
getDocument() |  | yii\mongodb\file\Download
getFilename() | Returns associated file's filename. | yii\mongodb\file\Download
getResource() | Returns persistent stream resource, which can be used to read file. | yii\mongodb\file\Download
getSize() | Returns the size of the associated file. | yii\mongodb\file\Download
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
setDocument() | Sets data of the document to be downloaded. | yii\mongodb\file\Download
substr() | Return part of a file. | yii\mongodb\file\Download
toFile() | Saves download to the physical file. | yii\mongodb\file\Download
toResource() | Returns an opened stream resource, which can be used to read file. | yii\mongodb\file\Download
toStream() | Saves file into the given stream. | yii\mongodb\file\Download
toString() | Returns a string of the bytes in the associated file. | yii\mongodb\file\Download
write() | Alias of toFile() method. | yii\mongodb\file\Download

## Property Details

$bytes public property

File content. This property is read-only.

public string $bytes = null
$chunkCursor public property

Chuck list cursor. This property is read-only.

public \MongoDB\Driver\Cursor $chunkCursor = null
$chunkIterator public property

Chuck cursor iterator. This property is read-only.

public Iterator $chunkIterator = null
$collection public property

File collection to be used.

public yii\mongodb\file\Collection $collection = null
$document public property

Document to be downloaded. Note that the type of this property differs in getter and setter. See getDocument() and setDocument() for details.

public array $document = null
$filename public property

File name. This property is read-only.

public string|null $filename = null
$resource public property

File stream resource. This property is read-only.

public resource $resource = null
$size public property

File size. This property is read-only.

public integer $size = null

## Method Details

getBytes() public method

Alias of toString() method.

public string getBytes ( )
return | string | File content.

getChunkCursor() public method

Returns file chunks read cursor.

public \MongoDB\Driver\Cursor getChunkCursor ( $refresh = false )
$refresh | boolean | Whether to recreate cursor, if it is already exist.
return | \MongoDB\Driver\Cursor | Chuck list cursor.
throws | yii\base\InvalidConfigException |

getChunkIterator() public method

Returns iterator for the file chunks cursor.

public Iterator getChunkIterator ( $refresh = false )
$refresh | boolean | Whether to recreate iterator, if it is already exist.
return | Iterator | Chuck cursor iterator.

getDocument() public method

public array getDocument ( )
return | array | Document to be downloaded.
throws | yii\base\InvalidConfigException | on invalid document configuration.

getFilename() public method

Returns associated file's filename.

public string|null getFilename ( )
return | string|null | File name.

getResource() public method

Returns persistent stream resource, which can be used to read file.

public resource getResource ( )
return | resource | File stream resource.

getSize() public method

Returns the size of the associated file.

public integer getSize ( )
return | integer | File size.

setDocument() public method

Sets data of the document to be downloaded.

Document can be specified by its ID, in this case its data will be fetched automatically via extra query.

public void setDocument ( $document )
$document | array|\MongoDB\BSON\ObjectID | Document raw data or document ID.

substr() public method

Return part of a file.

public string|false substr ( $start, $length )
$start | integer | Reading start position. If non-negative, the returned string will start at the start'th position in file, counting from zero. If negative, the returned string will start at the start'th character from the end of file.
$length | integer | Number of bytes to read. If given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of file). If given and is negative, then that many characters will be omitted from the end of file (after the start position has been calculated when a start is negative).
return | string|false | The extracted part of file or `false` on failure

toFile() public method

Saves download to the physical file.

public integer toFile ( $filename )
$filename | string | Name of the physical file.
return | integer | Number of written bytes.

toResource() public method

Returns an opened stream resource, which can be used to read file.

Note: each invocation of this method will create new file resource.

public resource toResource ( )
return | resource | Stream resource.

toStream() public method

Saves file into the given stream.

public integer toStream ( $stream )
$stream | resource | Stream, which file should be saved to.
return | integer | Number of written bytes.

toString() public method

Returns a string of the bytes in the associated file.

public string toString ( )
return | string | File content.

write() public method

Alias of toFile() method.

public integer write ( $filename )
$filename | string | Name of the physical file.
return | integer | Number of written bytes.