Source of: /manual/en/wrappers.php.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/wrappers.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'wrappers.php.php',
1 => 'PHP input/output streams',
),
'up' =>
array (
0 => 'wrappers.php',
1 => 'List of Supported Protocols/Wrappers',
),
'prev' =>
array (
0 => 'wrappers.ftp.php',
1 => 'FTP and FTPS',
),
'next' =>
array (
0 => 'wrappers.compression.php',
1 => 'Compression Streams',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="wrappers.php" class="section">
<h2 class="title">PHP input/output streams</h2>
<ul class="itemizedlist">
<li class="listitem"><span class="simpara"><var class="filename">php://stdin</var></span></li>
<li class="listitem"><span class="simpara"><var class="filename">php://stdout</var></span></li>
<li class="listitem"><span class="simpara"><var class="filename">php://stderr</var></span></li>
<li class="listitem"><span class="simpara"><var class="filename">php://output</var></span></li>
<li class="listitem"><span class="simpara"><var class="filename">php://input</var></span></li>
<li class="listitem"><span class="simpara"><var class="filename">php://filter</var> (available since PHP 5.0.0)</span></li>
<li class="listitem"><span class="simpara"><var class="filename">php://memory</var> (available since PHP 5.1.0)</span></li>
<li class="listitem"><span class="simpara"><var class="filename">php://temp</var> (available since PHP 5.1.0)</span></li>
</ul>
<p class="simpara">
<var class="filename">php://stdin</var>, <var class="filename">php://stdout</var>
and <var class="filename">php://stderr</var> allow direct access to
the corresponding input or output stream of the PHP process. The stream
references a duplicate file descriptor, so if you open
<var class="filename">php://stdin</var> and later close it, you close only your
copy of the descriptor--the actual stream referenced by
<b><tt class="constant">STDIN</tt></b> is unaffected. Note that PHP exhibited buggy
behavior in this regard until PHP 5.2.1. It is recommended that you simply
use the constants <b><tt class="constant">STDIN</tt></b>, <b><tt class="constant">STDOUT</tt></b>
and <b><tt class="constant">STDERR</tt></b> instead of manually opening streams using
these wrappers.
</p>
<p class="simpara">
<var class="filename">php://output</var> allows you to write to the
output buffer mechanism in the same way as
<a href="function.print.php" class="function">print()</a> and <a href="function.echo.php" class="function">echo()</a>.
</p>
<p class="simpara">
<var class="filename">php://input</var> allows you to read raw POST data.
It is a less memory intensive alternative to
<var class="varname"><a href="reserved.variables.httprawpostdata.php" class="classname">$HTTP_RAW_POST_DATA</a></var> and does not need any
special <var class="filename">php.ini</var> directives.
<var class="filename">php://input</var> is not available with
<i>enctype="multipart/form-data"</i>.
</p>
<p class="simpara">
<var class="filename">php://stdin</var> and
<var class="filename">php://input</var> are read-only, whereas
<var class="filename">php://stdout</var>,
<var class="filename">php://stderr</var> and
<var class="filename">php://output</var> are write-only.
</p>
<p class="simpara">
<var class="filename">php://filter</var> is a kind of meta-wrapper designed
to permit the application of filters to a stream at the time of
opening. This is useful with all-in-one file functions such as
<a href="function.readfile.php" class="function">readfile()</a>, <a href="function.file.php" class="function">file()</a>, and
<a href="function.file-get-contents.php" class="function">file_get_contents()</a> where there is otherwise
no opportunity to apply a filter to the stream prior the contents
being read.
</p>
<p class="simpara">
The <var class="filename">php://filter</var> target takes the following
'parameters' as parts of its 'path'.
</p>
<ul class="itemizedlist">
<li class="listitem">
<p class="para">
<i>/resource=<stream to be filtered></i>
(<em class="emphasis">required</em>) This parameter must be located at
the end of your <var class="filename">php://filter</var> specification and
should point to the stream which you want filtered.
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* This is equivalent to simply:<br /> readfile("http://www.example.com");<br /> since no filters are actually specified */<br /><br /></span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://filter/resource=http://www.example.com"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</li>
<li class="listitem">
<p class="para">
<i>/read=<filter list to apply to read chain></i>
(<em class="emphasis">optional</em>) This parameter takes one or more
filternames separated by the pipe character <i>|</i>.
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* This will output the contents of<br /> www.example.com entirely in uppercase */<br /></span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://filter/read=string.toupper/resource=http://www.example.com"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* This will do the same as above<br /> but will also ROT13 encode it */<br /></span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://filter/read=string.toupper|string.rot13/resource=http://www.example.com"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</li>
<li class="listitem">
<p class="para">
<i>/write=<filter list to apply to write chain></i>
(<em class="emphasis">optional</em>) This parameter takes one or more
filternames separated by the pipe character <i>|</i>.
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* This will filter the string "Hello World"<br /> through the rot13 filter, then write to<br /> example.txt in the current directory */<br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://filter/write=string.rot13/resource=example.txt"</span><span style="color: #007700">,</span><span style="color: #DD0000">"Hello World"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</li>
<li class="listitem">
<span class="simpara">
<i>/<filter list to apply to both chains></i>
(<em class="emphasis">optional</em>) Any filter lists which are not
prefixed specifically by <i>read=</i> or
<i>write=</i> will be applied to both the read and
write chains (as appropriate).
</span>
</li>
</ul>
<p class="simpara">
The <var class="filename">php://memory</var> wrapper stores the data in the
memory. <var class="filename">php://temp</var> behaves similarly, but uses a
temporary file for storing the data when a certain memory limit is reached
(the default is 2 MB).
</p>
<p class="simpara">
The <var class="filename">php://temp</var> wrapper takes the following
'parameters' as parts of its 'path':
</p>
<ul class="itemizedlist">
<li class="listitem">
<p class="para">
<i>/maxmemory:<number of bytes></i>
(<em class="emphasis">optional</em>). This parameter allows changing the
default value for the memory limit (when the data is moved to a temporary
file).
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$fiveMBs </span><span style="color: #007700">= </span><span style="color: #0000BB">5 </span><span style="color: #007700">* </span><span style="color: #0000BB">1024 </span><span style="color: #007700">* </span><span style="color: #0000BB">1024</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://temp/maxmemory:</span><span style="color: #0000BB">$fiveMBs</span><span style="color: #DD0000">"</span><span style="color: #007700">, </span><span style="color: #DD0000">'r+'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">fputs</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">"hello\n"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// read what we have written<br /></span><span style="color: #0000BB">rewind</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</li>
</ul>
<p class="para">
</p><table class="doctable table">
<caption><b>
Wrapper Summary (For <i>php://filter</i>,
refer to summary of wrapper being filtered.)
</b></caption>
<thead valign="middle">
<tr valign="middle">
<th>Attribute</th>
<th>Supported</th>
</tr>
</thead>
<tbody valign="middle" class="tbody">
<tr valign="middle">
<td align="left">Restricted by <a href="filesystem.configuration.php#ini.allow-url-fopen" class="link">allow_url_fopen</a></td>
<td align="left">No</td>
</tr>
<tr valign="middle">
<td align="left">Restricted by <a href="filesystem.configuration.php#ini.allow-url-include" class="link">allow_url_include</a></td>
<td align="left">
<i>php://input</i>,
<i>php://stdin</i>,
<i>php://memory</i> and
<i>php://temp</i> only.
</td>
</tr>
<tr valign="middle">
<td align="left">Allows Reading</td>
<td align="left">
<i>php://stdin</i>,
<i>php://input</i>,
<i>php://memory</i> and
<i>php://temp</i> only.
</td>
</tr>
<tr valign="middle">
<td align="left">Allows Writing</td>
<td align="left">
<i>php://stdout</i>,
<i>php://stderr</i>,
<i>php://output</i>,
<i>php://memory</i> and
<i>php://temp</i> only.
</td>
</tr>
<tr valign="middle">
<td align="left">Allows Appending</td>
<td align="left">
<i>php://stdout</i>,
<i>php://stderr</i>,
<i>php://output</i>,
<i>php://memory</i> and
<i>php://temp</i> only. (Equivalent to writing)
</td>
</tr>
<tr valign="middle">
<td align="left">Allows Simultaneous Reading and Writing</td>
<td align="left">
<i>php://memory</i> and
<i>php://temp</i> only.
</td>
</tr>
<tr valign="middle">
<td align="left">Supports <a href="function.stat.php" class="function">stat()</a></td>
<td align="left">
<i>php://memory</i> and
<i>php://temp</i> only.
</td>
</tr>
<tr valign="middle">
<td align="left">Supports <a href="function.unlink.php" class="function">unlink()</a></td>
<td align="left">No</td>
</tr>
<tr valign="middle">
<td align="left">Supports <a href="function.rename.php" class="function">rename()</a></td>
<td align="left">No</td>
</tr>
<tr valign="middle">
<td align="left">Supports <a href="function.mkdir.php" class="function">mkdir()</a></td>
<td align="left">No</td>
</tr>
<tr valign="middle">
<td align="left">Supports <a href="function.rmdir.php" class="function">rmdir()</a></td>
<td align="left">No</td>
</tr>
</tbody>
</table>
<p>
</p>
</div><?php manual_footer(); ?>