📄 function.stream-filter-append.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Attach a filter to a stream</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="function.stream-encoding.html">stream_encoding</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.stream-filter-prepend.html">stream_filter_prepend</a></div> <div class="up"><a href="ref.stream.html">Stream Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.stream-filter-append" class="refentry"> <div class="refnamediv"> <h1 class="refname">stream_filter_append</h1> <p class="verinfo">(PHP 4 >= 4.3.0, PHP 5)</p><p class="refpurpose"><span class="refname">stream_filter_append</span> — <span class="dc-title">Attach a filter to a stream</span></p> </div> <div class="refsect1 unknown"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><b><b>stream_filter_append</b></b></span> ( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$stream</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$filtername</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$read_write</tt></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$params</tt></span> ]] )</div> <p class="para rdfs-comment"> Adds <i><tt class="parameter">filtername</tt></i> to the list of filters attached to <i><tt class="parameter">stream</tt></i>. This filter will be added with the specified <i><tt class="parameter">params</tt></i> to the <em class="emphasis">end</em> of the list and will therefore be called last during stream operations. To add a filter to the beginning of the list, use <a href="function.stream-filter-prepend.html" class="function">stream_filter_prepend()</a>. </p> <p class="para"> By default, <b>stream_filter_append()</b> will attach the filter to the <i>read filter chain</i> if the file was opened for reading (i.e. File Mode: <i>r</i>, and/or <i>+</i>). The filter will also be attached to the <i>write filter chain</i> if the file was opened for writing (i.e. File Mode: <i>w</i>, <i>a</i>, and/or <i>+</i>). <b><tt>STREAM_FILTER_READ</tt></b>, <b><tt>STREAM_FILTER_WRITE</tt></b>, and/or <b><tt>STREAM_FILTER_ALL</tt></b> can also be passed to the <i><tt class="parameter">read_write</tt></i> parameter to override this behavior. </p> <p class="para"> As of PHP 5.1.0, this function returns a resource which can be used to refer to this filter instance during a call to <a href="function.stream-filter-remove.html" class="function">stream_filter_remove()</a>. Prior to PHP 5.1.0, this function returns <b><tt>TRUE</tt></b> on success or <b><tt>FALSE</tt></b> on failure. </p> <p class="para"> <div class="example"> <p><b>Example #1 Controlling where filters are applied</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* Open a test file for reading and writing */<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">'test.txt'</span><span style="color: #007700">, </span><span style="color: #DD0000">'w+'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Apply the ROT13 filter to the<br /> * write filter chain, but not the<br /> * read filter chain */<br /></span><span style="color: #0000BB">stream_filter_append</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">"string.rot13"</span><span style="color: #007700">, </span><span style="color: #0000BB">STREAM_FILTER_WRITE</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Write a simple string to the file<br /> * it will be ROT13 transformed on the<br /> * way out */<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">"This is a test\n"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Back up to the beginning of the file */<br /></span><span style="color: #0000BB">rewind</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Read the contents of the file back out.<br /> * Had the filter been applied to the<br /> * read filter chain as well, we would see<br /> * the text ROT13ed back to its original state */<br /></span><span style="color: #0000BB">fpassthru</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Expected Output<br /> ---------------<br /><br />Guvf vf n grfg<br /><br /> */<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <blockquote><p><b class="note">Note</b>: <b>When using custom (user) filters</b><br /> <span class="simpara"> <a href="function.stream-filter-register.html" class="function">stream_filter_register()</a> must be called first in order to register the desired user filter to <i><tt class="parameter">filtername</tt></i>. </span> </p></blockquote> <blockquote><p><b class="note">Note</b>: <span class="simpara"> Stream data is read from resources (both local and remote) in chunks, with any unconsumed data kept in internal buffers. When a new filter is appended to a stream, data in the internal buffers is processed through the new filter at that time. This differs from the behavior of <a href="function.stream-filter-prepend.html" class="function">stream_filter_prepend()</a>. </span> </p></blockquote> <p class="simpara"> See also <a href="function.stream-filter-register.html" class="function">stream_filter_register()</a>, <a href="function.stream-filter-prepend.html" class="function">stream_filter_prepend()</a>, and <a href="function.stream-get-filters.html" class="function">stream_get_filters()</a>. </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.stream-encoding.html">stream_encoding</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.stream-filter-prepend.html">stream_filter_prepend</a></div> <div class="up"><a href="ref.stream.html">Stream Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -