PHP Class yii\mongodb\file\Download

A Download object is usually created by calling [[Collection::get()]] or [[Collection::createDownload()]]. Usage example: php 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: php $filePart = Yii::$app->mongodb->getFileCollection()->createDownload($document['_id'])->substr(256, 1024);
Since: 2.1
Author: Paul Klimov ([email protected])
Inheritance: extends yii\base\Object
Show file Open project: yiisoft/yii2-mongodb Class Usage Examples

Public Properties

Property Type Description
$collection file collection to be used.

Public Methods

Method Description
getBytes ( ) : string Alias of Download::toString method.
getChunkCursor ( boolean $refresh = false ) : MongoDB\Driver\Cursor Returns file chunks read cursor.
getChunkIterator ( boolean $refresh = false ) : Iterator Returns iterator for the file chunks cursor.
getDocument ( ) : array
getFilename ( ) : string | null Returns associated file's filename.
getResource ( ) : resource Returns persistent stream resource, which can be used to read file.
getSize ( ) : integer Returns the size of the associated file.
setDocument ( array | MongoDB\BSON\ObjectID $document ) Sets data of the document to be downloaded.
substr ( integer $start, integer $length ) : string | false Return part of a file.
toFile ( string $filename ) : integer Saves download to the physical file.
toResource ( ) : resource Returns an opened stream resource, which can be used to read file.
toStream ( resource $stream ) : integer Saves file into the given stream.
toString ( ) : string Returns a string of the bytes in the associated file.
write ( string $filename ) : integer Alias of Download::toFile method.

Method Details

getBytes() public method

Alias of Download::toString method.
public getBytes ( ) : string
return string file content.

getChunkCursor() public method

Returns file chunks read cursor.
public getChunkCursor ( boolean $refresh = false ) : MongoDB\Driver\Cursor
$refresh boolean whether to recreate cursor, if it is already exist.
return MongoDB\Driver\Cursor chuck list cursor.

getChunkIterator() public method

Returns iterator for the file chunks cursor.
public getChunkIterator ( boolean $refresh = false ) : Iterator
$refresh boolean whether to recreate iterator, if it is already exist.
return Iterator chuck cursor iterator.

getDocument() public method

public getDocument ( ) : array
return array document to be downloaded.

getFilename() public method

Returns associated file's filename.
public getFilename ( ) : string | null
return string | null file name.

getResource() public method

Returns persistent stream resource, which can be used to read file.
public getResource ( ) : resource
return resource file stream resource.

getSize() public method

Returns the size of the associated file.
public getSize ( ) : integer
return integer file size.

setDocument() public method

Document can be specified by its ID, in this case its data will be fetched automatically via extra query.
public setDocument ( array | MongoDB\BSON\ObjectID $document )
$document array | MongoDB\BSON\ObjectID document raw data or document ID.

substr() public method

Return part of a file.
public substr ( integer $start, integer $length ) : string | false
$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 toFile ( string $filename ) : integer
$filename string name of the physical file.
return integer number of written bytes.

toResource() public method

Note: each invocation of this method will create new file resource.
public toResource ( ) : resource
return resource stream resource.

toStream() public method

Saves file into the given stream.
public toStream ( resource $stream ) : integer
$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 toString ( ) : string
return string file content.

write() public method

Alias of Download::toFile method.
public write ( string $filename ) : integer
$filename string name of the physical file.
return integer number of written bytes.

Property Details

$collection public property

file collection to be used.
public $collection