downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

MongoGridFS::storeUpload> <MongoGridFS::storeBytes
[edit] Last updated: Fri, 17 May 2013

view this page in

MongoGridFS::storeFile

(PECL mongo >=0.9.0)

MongoGridFS::storeFileAlmacena un fichero en la base de datos

Descripción

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

Parámetros

filename

El nombre del fichero a almacenar.

metadata

Otros campos de metadatos a incluir en documento de fichero.

Nota:

Estos cambpos podrían sobrescribir aquellos que serían creados automáticamente por el controlador, tal como está descrito es la documentación del núcleo de MongoDB para las » colecciones de ficheros. Algunos casos prácticos para este comportamiento serían especificar un chunkSize o _id personalizados para un fichero.

options

Opciones para el almacenamiento.

Valores devueltos

Devuelve el _id del documento de fichero guardado. Será un MongoId generado a menos que unless sea especificado explícitamente un _id en el parámetro extra.

Errores/Excepciones

Lanza una MongoCursorException si la opción "w" está establecida y la escritura falla.

Lanza una MongoCursorTimeoutException si la opción "w" está establecida a un valor mayor que uno y la operación toma más de MongoCursor::$timeout milisegundos en completarse. Esto no pondrá fin a la operación en el servidor, es un tiempo de espera del lado del cliente. La operación en MongoCollection::$wtimeout es milisegundos.

Ejemplos

Ejemplo #1 MongoGridFS::storeFile() con metadatos adicionales

<?php
$m 
= new MongoClient();
$gridfs $m->selectDB('test')->getGridFS();

$id $gridfs->storeFile('example.txt', array('contentType' => 'plain/text'));
$gridfsFile $gridfs->get($id);

var_dump($gridfsFile->file);
?>

El resultado del ejemplo sería algo similar a:

array(7) {
  ["_id"]=>
  object(MongoId)#6 (0) {
  }
  ["contentType"]=>
  string(10) "plain/text"
  ["filename"]=>
  string(11) "example.txt"
  ["uploadDate"]=>
  object(MongoDate)#7 (0) {
  }
  ["length"]=>
  int(26)
  ["chunkSize"]=>
  int(262144)
  ["md5"]=>
  string(32) "c3fcd3d76192e4007dfb496cca67e13b"
}

Ver también



add a note add a note User Contributed Notes MongoGridFS::storeFile - [2 notes]
up
0
mike at eastghost dot com
1 year ago
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.
up
-1
mike at eastghost dot com
1 year ago
A little confusing, but the "string $filename" (i.e., the first parameter) is the actual data that gets stored into the GridFS.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites