MongoGridFS
在线手册:中文 英文
PHP手册

MongoGridFS::storeFile

(PECL mongo >=0.9.0)

MongoGridFS::storeFileStores a file in the database

说明

public mixed MongoGridFS::storeFile ( string $filename [, array $extra = array() [, array $options = array() ]] )

参数

filename

The name of the file.

extra

Other metadata to add to the file saved.

options

Options for the store.

  • "safe"

    Check that this store succeeded.

返回值

Returns the _id of the saved object.

错误/异常

Throws MongoCursorException if the "safe" option is set and the insert fails.


MongoGridFS
在线手册:中文 英文
PHP手册
PHP手册 - N: Stores a file in the database

用户评论:

mike at eastghost dot com (16-Feb-2012 09:50)

Pass

$options['safe'] = true;

to make Mongo slow down and do inserts sequentially instead of in parallel.

Use case:

I had 274 PDF documents stored on ext3 disk (avg. size was 5MB, none over 20MB)

A PHP loop loaded each PDF, then did a $GridFS->storeFile( $data, $meta );

This caused server to spike from load average of 0.2 to over 20.0

Apparently what was happening is the loop slammed Mongo (then memory, disk) with 274 convert/stores to do all at once.

Adding

$GridFS->storeFile( $data, $meta, array( 'safe' => true ) );

made the loop slow down and wait for each insert to finish and be confirmed before going on to next PDF.

Moral: Safe option can be used to maintain sanity and is not always just for data validity.

mike at eastghost dot com (16-Feb-2012 09:31)

A little confusing, but the "string $filename" (i.e., the first parameter) is the actual data that gets stored into the GridFS.