php-doc-en/reference/mongo/mongogridfs/storeupload.xml

167 lines
4.5 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mongogridfs.storeupload" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>MongoGridFS::storeUpload</refname>
<refpurpose>Stores an uploaded file in the database</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>mixed</type><methodname>MongoGridFS::storeUpload</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>metadata</parameter></methodparam>
</methodsynopsis>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term>
<parameter>name</parameter>
</term>
<listitem>
<para>
The name of the uploaded file(s) to store. This should correspond to the
file field's <literal>name</literal> attribute in the HTML form.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<parameter>metadata</parameter>
</term>
<listitem>
<para>
Other metadata fields to include in the file document.
</para>
&mongo.gridfs.store.metadata.note;
<note>
<para>
The <literal>filename</literal> field will be populated with the
client's filename (e.g. <literal>$_FILES['foo']['name']</literal>).
</para>
</note>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
&mongo.gridfs.store.return;
<note>
<para>
If <link linkend="features.file-upload.multiple">multiple files are uploaded
using the same field name</link>, this method will not return anything;
however, the files themselves will still be processed.
</para>
</note>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
Throws <classname>MongoGridFSException</classname> if there is an error
reading the uploaded file(s) or inserting into the <literal>chunks</literal>
or <literal>files</literal> collections.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>1.2.5</entry>
<entry>
Changed second parameter to an array of metadata. Pre-1.2.5, the
second parameter was an optional string overriding the filename.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>MongoGridFS::storeUpload</function> HTML form example</title>
<para>
Suppose you have the following HTML form:
</para>
<programlisting role="html">
<![CDATA[
<form method="POST" enctype="multipart/form-data">
<label for="username">Username:</label>
<input type="text" name="username" id="username" />
<label for="pic">Please upload a profile picture:</label>
<input type="file" name="pic" id="pic" />
<input type="submit" />
</form>
]]>
</programlisting>
<para>
If you wanted to store the uploaded image in MongoDB, you could do the
following in the script handling the form submission:
</para>
<programlisting role="php">
<![CDATA[
<?php
$m = new MongoClient();
$gridfs = $m->selectDB('test')->getGridFS();
$gridfs->storeUpload('pic', array('username' => $_POST['username']));
?>
]]>
</programlisting>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><function>MongoGridFS::put</function></member>
<member><function>MongoGridFS::storeBytes</function></member>
<member><function>MongoGridFS::storeFile</function></member>
<member>MongoDB core docs on <link xlink:href="&url.mongodb.docs.gridfs;">GridFS</link></member>
</simplelist>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->