<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Accessing various I/O streams</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="wrappers.ftp.html">ftp://</a></div> <div class="next" style="text-align: right; float: right;"><a href="wrappers.compression.html">zlib://</a></div> <div class="up"><a href="wrappers.html">Supported Protocols and Wrappers</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="wrappers.php" class="refentry"> <div class="refnamediv"> <h1 class="refname">php://</h1> <p class="refpurpose"><span class="refname">php://</span> — <span class="dc-title">Accessing various I/O streams</span></p> </div> <div class="refsect1 description" id="refsect1-wrappers.php-description"> <h3 class="title">Description</h3> <p class="para"> PHP provides a number of miscellaneous I/O streams that allow access to PHP's own input and output streams, the standard input, output and error file descriptors, in-memory and disk-backed temporary file streams, and filters that can manipulate other file resources as they are read from and written to. </p> <div class="refsect2 unknown-descriptioo" id="refsect2-wrappers.php-unknown-descriptioo"> <h4 class="title">php://stdin, php://stdout and php://stderr</h4> <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 <strong><code>STDIN</code></strong> 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 <strong><code>STDIN</code></strong>, <strong><code>STDOUT</code></strong> and <strong><code>STDERR</code></strong> instead of manually opening streams using these wrappers. </p> <p class="simpara"> <var class="filename">php://stdin</var> is read-only, whereas <var class="filename">php://stdout</var> and <var class="filename">php://stderr</var> are write-only. </p> </div> <div class="refsect2 unknown-unknown-descriptiop" id="refsect2-wrappers.php-unknown-unknown-descriptiop"> <h4 class="title">php://input</h4> <p class="simpara"> <var class="filename">php://input</var> is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use <var class="filename">php://input</var> instead of <var class="varname"><var class="varname"><a href="reserved.variables.httprawpostdata.html" class="classname">$HTTP_RAW_POST_DATA</a></var></var> as it does not depend on special <var class="filename">php.ini</var> directives. Moreover, for those cases where <var class="varname"><var class="varname"><a href="reserved.variables.httprawpostdata.html" class="classname">$HTTP_RAW_POST_DATA</a></var></var> is not populated by default, it is a potentially less memory intensive alternative to activating <a href="ini.core.html#ini.always-populate-raw-post-data" class="link">always_populate_raw_post_data</a>. <var class="filename">php://input</var> is not available with <em>enctype="multipart/form-data"</em>. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> A stream opened with <var class="filename">php://input</var> can only be read once; the stream does not support seek operations. However, depending on the SAPI implementation, it may be possible to open another <var class="filename">php://input</var> stream and restart reading. This is only possible if the request body data has been saved. Typically, this is the case for POST requests, but not other request methods, such as PUT or PROPFIND. </span> </p></blockquote> </div> <div class="refsect2 unknown-unknown-unknown-descriptioq" id="refsect2-wrappers.php-unknown-unknown-unknown-descriptioq"> <h4 class="title">php://output</h4> <p class="para"> <var class="filename">php://output</var> is a write-only stream that allows you to write to the output buffer mechanism in the same way as <span class="function"><a href="function.print.html" class="function">print</a></span> and <span class="function"><a href="function.echo.html" class="function">echo</a></span>. </p> </div> <div class="refsect2 unknown-unknown-unknown-unknown-descriptior" id="refsect2-wrappers.php-unknown-unknown-unknown-unknown-descriptior"> <h4 class="title">php://fd</h4> <p class="simpara"> <var class="filename">php://fd</var> allows direct access to the given file descriptor. For example, <var class="filename">php://fd/3</var> refers to file descriptor 3. </p> </div> <div class="refsect2 unknown-unknown-unknown-unknown-unknown-descriptios" id="refsect2-wrappers.php-unknown-unknown-unknown-unknown-unknown-descriptios"> <h4 class="title">php://memory and php://temp</h4> <p class="simpara"> <var class="filename">php://memory</var> and <var class="filename">php://temp</var> are read-write streams that allow temporary data to be stored in a file-like wrapper. The only difference between the two is that <var class="filename">php://memory</var> will always store its data in memory, whereas <var class="filename">php://temp</var> will use a temporary file once the amount of data stored hits a predefined limit (the default is 2 MB). The location of this temporary file is determined in the same way as the <span class="function"><a href="function.sys-get-temp-dir.html" class="function">sys_get_temp_dir()</a></span> function. </p> <p class="simpara"> The memory limit of <var class="filename">php://temp</var> can be controlled by appending <em>/maxmemory:NN</em>, where <em>NN</em> is the maximum amount of data to keep in memory before using a temporary file, in bytes. </p> </div> <div class="refsect2 unknown-unknown-unknown-unknown-unknown-unknown-descriptiot" id="refsect2-wrappers.php-unknown-unknown-unknown-unknown-unknown-unknown-descriptiot"> <h4 class="title">php://filter</h4> <p class="simpara"> <var class="filename">php://filter</var> is a kind of meta-wrapper designed to permit the application of <a href="filters.html" class="link">filters</a> to a stream at the time of opening. This is useful with all-in-one file functions such as <span class="function"><a href="function.readfile.html" class="function">readfile()</a></span>, <span class="function"><a href="function.file.html" class="function">file()</a></span>, and <span class="function"><a href="function.file-get-contents.html" class="function">file_get_contents()</a></span> where there is otherwise no opportunity to apply a filter to the stream prior the contents being read. </p> <p class="para"> The <var class="filename">php://filter</var> target takes the following parameters as part of its path. Multiple filter chains can be specified on one path. Please refer to the examples for specifics on using these parameters. </p> <p class="para"> <table class="doctable table"> <caption><strong>php://filter parameters</strong></caption> <thead> <tr> <th>Name</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td> <em>resource=<stream to be filtered></em> </td> <td> This parameter is required. It specifies the stream that you would like to filter. </td> </tr> <tr> <td> <em>read=<filter list to apply to read chain></em> </td> <td> This parameter is optional. One or more filter names can be provided here, separated by the pipe character (<em>|</em>). </td> </tr> <tr> <td> <em>write=<filter list to apply to write chain></em> </td> <td> This parameter is optional. One or more filter names can be provided here, separated by the pipe character (<em>|</em>). </td> </tr> <tr> <td> <em><filter list to apply to both chains></em> </td> <td> Any filter lists which are not prefixed by <em>read=</em> or <em>write=</em> will be applied to both the read and write chains as appropriate. </td> </tr> </tbody> </table> </p> </div> </div> <div class="refsect1 options" id="refsect1-wrappers.php-options"> <h3 class="title">Options</h3> <p class="para"> <table class="doctable table"> <caption><strong> Wrapper Summary (for <em>php://filter</em>, refer to the summary of the wrapper being filtered) </strong></caption> <thead> <tr> <th>Attribute</th> <th>Supported</th> </tr> </thead> <tbody class="tbody"> <tr> <td>Restricted by <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">allow_url_fopen</a></td> <td>No</td> </tr> <tr> <td>Restricted by <a href="filesystem.configuration.html#ini.allow-url-include" class="link">allow_url_include</a></td> <td> <em>php://input</em>, <em>php://stdin</em>, <em>php://memory</em> and <em>php://temp</em> only. </td> </tr> <tr> <td>Allows Reading</td> <td> <em>php://stdin</em>, <em>php://input</em>, <em>php://fd</em>, <em>php://memory</em> and <em>php://temp</em> only. </td> </tr> <tr> <td>Allows Writing</td> <td> <em>php://stdout</em>, <em>php://stderr</em>, <em>php://output</em>, <em>php://fd</em>, <em>php://memory</em> and <em>php://temp</em> only. </td> </tr> <tr> <td>Allows Appending</td> <td> <em>php://stdout</em>, <em>php://stderr</em>, <em>php://output</em>, <em>php://fd</em>, <em>php://memory</em> and <em>php://temp</em> only. (Equivalent to writing) </td> </tr> <tr> <td>Allows Simultaneous Reading and Writing</td> <td> <em>php://fd</em>, <em>php://memory</em> and <em>php://temp</em> only. </td> </tr> <tr> <td>Supports <span class="function"><a href="function.stat.html" class="function">stat()</a></span></td> <td> <em>php://memory</em> and <em>php://temp</em> only. </td> </tr> <tr> <td>Supports <span class="function"><a href="function.unlink.html" class="function">unlink()</a></span></td> <td>No</td> </tr> <tr> <td>Supports <span class="function"><a href="function.rename.html" class="function">rename()</a></span></td> <td>No</td> </tr> <tr> <td>Supports <span class="function"><a href="function.mkdir.html" class="function">mkdir()</a></span></td> <td>No</td> </tr> <tr> <td>Supports <span class="function"><a href="function.rmdir.html" class="function">rmdir()</a></span></td> <td>No</td> </tr> <tr> <td>Supports <span class="function"><a href="function.stream-select.html" class="function">stream_select()</a></span></td> <td> <em>php://stdin</em>, <em>php://stdout</em>, <em>php://stderr</em>, <em>php://fd</em> and <em>php://temp</em> only. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 changelog" id="refsect1-wrappers.php-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.3.6</td> <td> <var class="filename">php://fd</var> was added. </td> </tr> <tr> <td>5.1.0</td> <td> <var class="filename">php://memory</var> and <var class="filename">php://temp</var> were added. </td> </tr> <tr> <td>5.0.0</td> <td> <var class="filename">php://filter</var> was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-wrappers.php-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-316"> <p><strong>Example #1 php://temp/maxmemory</strong></p> <div class="example-contents"><p> This optional parameter allows setting the memory limit before <var class="filename">php://temp</var> starts using a temporary file. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Set the limit to 5 MB.<br /></span><span style="color: #0000BB">$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> <div class="example" id="example-317"> <p><strong>Example #2 php://filter/resource=<stream to be filtered></strong></p> <div class="example-contents"><p> 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> <div class="example-contents"> <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> <div class="example" id="example-318"> <p><strong>Example #3 php://filter/read=<filter list to apply to read chain></strong></p> <div class="example-contents"><p> This parameter takes one or more filternames separated by the pipe character <em>|</em>. </p></div> <div class="example-contents"> <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> <div class="example" id="example-319"> <p><strong>Example #4 php://filter/write=<filter list to apply to write chain></strong></p> <div class="example-contents"><p> This parameter takes one or more filternames separated by the pipe character <em>|</em>. </p></div> <div class="example-contents"> <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> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="wrappers.ftp.html">ftp://</a></div> <div class="next" style="text-align: right; float: right;"><a href="wrappers.compression.html">zlib://</a></div> <div class="up"><a href="wrappers.html">Supported Protocols and Wrappers</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>