1
- ============================================
1
+ ===========================================================
2
2
MongoDB\\GridFS\\Bucket::registerGlobalStreamWrapperAlias()
3
- ============================================
3
+ ===========================================================
4
4
5
5
.. versionadded:: 1.18
6
6
@@ -17,14 +17,13 @@ Definition
17
17
18
18
.. phpmethod:: MongoDB\\GridFS\\Bucket::registerGlobalStreamWrapperAlias()
19
19
20
- Register an alias to enable basic filename access for a GridFS bucket
20
+ Registers an alias for the bucket, which enables files within the bucket to
21
+ be accessed using a basic filename string (e.g.
22
+ `gridfs://<bucket-alias>/<filename>`).
21
23
22
24
.. code-block:: php
23
25
24
- function registerGlobalStreamWrapperAlias(
25
- string $alias
26
- ): void
27
-
26
+ function registerGlobalStreamWrapperAlias(string $alias): void
28
27
29
28
Parameters
30
29
----------
@@ -33,25 +32,43 @@ Parameters
33
32
A non-empty string used to identify the GridFS bucket when accessing files
34
33
using the ``gridfs://`` stream wrapper.
35
34
36
-
37
-
38
35
Behavior
39
36
--------
40
37
41
- Files can then be accessed using the following pattern: ``gridfs://<bucket-alias>/<filename>``
38
+ After registering an alias for the bucket, the most recent revision of a file
39
+ can be accessed using a filename string in the form ``gridfs://<bucket-alias>/<filename>``.
42
40
43
41
Supported stream functions:
44
42
43
+ - ``copy()``
45
44
- ``file_exists()``
46
45
- ``file_get_contents()``
47
46
- ``file_put_contents()``
48
47
- ``filemtime()``
49
48
- ``filesize()``
49
+ - ``file()``
50
50
- ``fopen()`` (with "r", "rb", "w", and "wb" modes)
51
51
52
+ In read mode, the stream context can contain the option ``gridfs['revision']``
53
+ to specify the revision number of the file to read. If omitted, the most recent
54
+ revision is read (revision ``-1``).
55
+
56
+ In write mode, the stream context can contain the options ``gridfs['chunkSizeBytes']``
57
+ and ``gridfs['disableMD5']`` options. If omitted, the defaults are inherited from the
58
+ ``Bucket`` instance options.
59
+
52
60
Example
53
61
-------
54
62
63
+ Read and write to a GridFS bucket using the ``gridfs://`` stream wrapper
64
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65
+
66
+ The following example demonstrates how to register an alias for a GridFS bucket
67
+ and use the functions ``file_exists()``, ``file_get_contents()``, and
68
+ ``file_put_contents()`` to read and write to the bucket.
69
+
70
+ Each call to these functions makes a request to the server.
71
+
55
72
.. code-block:: php
56
73
57
74
<?php
@@ -77,4 +94,40 @@ The output would then resemble:
77
94
bool(true)
78
95
Hello, GridFS!
79
96
97
+ Read a specific revision of a file
98
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99
+
100
+ Using a stream context, you can specify the revision number of the file to
101
+ read. If omitted, the most recent revision is read.
102
+
103
+ .. code-block:: php
104
+
105
+ <?php
106
+
107
+ $database = (new MongoDB\Client)->selectDatabase('test');
108
+ $bucket = $database->selectGridFSBucket();
109
+
110
+ $bucket->registerGlobalStreamWrapperAlias('mybucket');
111
+
112
+ // Creating revision 0
113
+ $handle = fopen('gridfs://mybucket/hello.txt', 'w');
114
+ fwrite($handle, 'Hello, GridFS! (v0)');
115
+ fclose($handle);
116
+
117
+ // Creating revision 1
118
+ $handle = fopen('gridfs://mybucket/hello.txt', 'w');
119
+ fwrite($handle, 'Hello, GridFS! (v1)');
120
+ fclose($handle);
121
+
122
+ // Read revision 0
123
+ $context = stream_context_create([
124
+ 'gridfs' => ['revision' => 0],
125
+ ]);
126
+ $handle = fopen('gridfs://mybucket/hello.txt', 'r', false, $context);
127
+ echo fread($handle, 1024);
128
+
129
+ The output would then resemble:
130
+
131
+ .. code-block:: none
80
132
133
+ Hello, GridFS! (v0)
0 commit comments