wrappers.php.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 276 行
HTML
276 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>PHP input/output streams</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="wrappers.ftp.html">FTP and FTPS</a></div> <div class="next" style="text-align: right; float: right;"><a href="wrappers.compression.html">Compression Streams</a></div> <div class="up"><a href="wrappers.html">List of Supported Protocols/Wrappers</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><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 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>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>STDIN</tt></b>, <b><tt>STDOUT</tt></b> and <b><tt>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.html" class="function">print()</a> and <a href="function.echo.html" 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.html" 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.html" class="function">readfile()</a>, <a href="function.file.html" class="function">file()</a>, and <a href="function.file-get-contents.html" 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. <div class="informalexample"> <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> </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>. <div class="informalexample"> <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> </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>. <div class="informalexample"> <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> </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). <div class="informalexample"> <div class="example-contents"><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:$fiveMBs"</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> </li> </ul> <p class="para"> <table border="5"> <caption><b> Wrapper Summary (For <i>php://filter</i>, refer to summary of wrapper being filtered.) </b></caption> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Attribute</th> <th colspan="1">Supported</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">Restricted by <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">allow_url_fopen</a></td> <td colspan="1" rowspan="1" align="left">No</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">Restricted by <a href="filesystem.configuration.html#ini.allow-url-include" class="link">allow_url_include</a></td> <td colspan="1" rowspan="1" 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 colspan="1" rowspan="1" align="left">Allows Reading</td> <td colspan="1" rowspan="1" 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 colspan="1" rowspan="1" align="left">Allows Writing</td> <td colspan="1" rowspan="1" 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 colspan="1" rowspan="1" align="left">Allows Appending</td> <td colspan="1" rowspan="1" 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 colspan="1" rowspan="1" align="left">Allows Simultaneous Reading and Writing</td> <td colspan="1" rowspan="1" align="left"> <i>php://memory</i> and <i>php://temp</i> only. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">Supports <a href="function.stat.html" class="function">stat()</a></td> <td colspan="1" rowspan="1" align="left"> <i>php://memory</i> and <i>php://temp</i> only. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">Supports <a href="function.unlink.html" class="function">unlink()</a></td> <td colspan="1" rowspan="1" align="left">No</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">Supports <a href="function.rename.html" class="function">rename()</a></td> <td colspan="1" rowspan="1" align="left">No</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">Supports <a href="function.mkdir.html" class="function">mkdir()</a></td> <td colspan="1" rowspan="1" align="left">No</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">Supports <a href="function.rmdir.html" class="function">rmdir()</a></td> <td colspan="1" rowspan="1" align="left">No</td> </tr> </tbody> </colgroup> </table> </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="wrappers.ftp.html">FTP and FTPS</a></div> <div class="next" style="text-align: right; float: right;"><a href="wrappers.compression.html">Compression Streams</a></div> <div class="up"><a href="wrappers.html">List of Supported Protocols/Wrappers</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?