<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >stream_filter_append</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Stream Functions" HREF="ref.stream.html"><LINK REL="PREVIOUS" TITLE="stream_encoding" HREF="function.stream-encoding.html"><LINK REL="NEXT" TITLE="stream_filter_prepend" HREF="function.stream-filter-prepend.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.stream-encoding.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.stream-filter-prepend.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.stream-filter-append" ></A >stream_filter_append</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN222900" ></A ><P > (PHP 4 >= 4.3.0, PHP 5)</P >stream_filter_append -- Attach a filter to a stream</DIV ><DIV CLASS="refsect1" ><A NAME="AEN222903" ></A ><H2 >Description</H2 >resource <B CLASS="methodname" >stream_filter_append</B > ( resource stream, string filtername [, int read_write [, mixed params]] )<BR ></BR ><P > Adds <CODE CLASS="parameter" >filtername</CODE > to the list of filters attached to <CODE CLASS="parameter" >stream</CODE >. This filter will be added with the specified <CODE CLASS="parameter" >params</CODE > to the <SPAN CLASS="emphasis" ><I CLASS="emphasis" >end</I ></SPAN > 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" ><B CLASS="function" >stream_filter_prepend()</B ></A >. </P ><P > By default, <B CLASS="function" >stream_filter_append()</B > will attach the filter to the <TT CLASS="literal" >read filter chain</TT > if the file was opened for reading (i.e. File Mode: <TT CLASS="literal" >r</TT >, and/or <TT CLASS="literal" >+</TT >). The filter will also be attached to the <TT CLASS="literal" >write filter chain</TT > if the file was opened for writing (i.e. File Mode: <TT CLASS="literal" >w</TT >, <TT CLASS="literal" >a</TT >, and/or <TT CLASS="literal" >+</TT >). <TT CLASS="constant" ><B >STREAM_FILTER_READ</B ></TT >, <TT CLASS="constant" ><B >STREAM_FILTER_WRITE</B ></TT >, and/or <TT CLASS="constant" ><B >STREAM_FILTER_ALL</B ></TT > can also be passed to the <CODE CLASS="parameter" >read_write</CODE > parameter to override this behavior. </P ><P > 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" ><B CLASS="function" >stream_filter_remove()</B ></A >. Prior to PHP 5.1.0, this function returns <TT CLASS="constant" ><B >TRUE</B ></TT > on success or <TT CLASS="constant" ><B >FALSE</B ></TT > on failure. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN222944" ></A ><P ><B >例 1. Controlling where filters are applied</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">/* Open a test file for reading and writing */<br /></font><font color="#0000BB">$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">'test.txt'</font><font color="#007700">, </font><font color="#DD0000">'w+'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Apply the ROT13 filter to the<br /> * write filter chain, but not the<br /> * read filter chain */<br /></font><font color="#0000BB">stream_filter_append</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#DD0000">"string.rot13"</font><font color="#007700">, </font><font color="#0000BB">STREAM_FILTER_WRITE</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Write a simple string to the file<br /> * it will be ROT13 transformed on the<br /> * way out */<br /></font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#DD0000">"This is a test\n"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Back up to the beginning of the file */<br /></font><font color="#0000BB">rewind</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br /><br /></font><font 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 /></font><font color="#0000BB">fpassthru</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br /><br /></font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Expected Output<br /> ---------------<br /><br />Guvf vf n grfg<br /><br /> */<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >When using custom (user) filters: </B > <A HREF="function.stream-filter-register.html" ><B CLASS="function" >stream_filter_register()</B ></A > must be called first in order to register the desired user filter to <CODE CLASS="parameter" >filtername</CODE >. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > 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" ><B CLASS="function" >stream_filter_prepend()</B ></A >. </P ></BLOCKQUOTE ></DIV ><P > See also <A HREF="function.stream-filter-register.html" ><B CLASS="function" >stream_filter_register()</B ></A >, <A HREF="function.stream-filter-prepend.html" ><B CLASS="function" >stream_filter_prepend()</B ></A >, and <A HREF="function.stream-get-filters.html" ><B CLASS="function" >stream_get_filters()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.stream-encoding.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.stream-filter-prepend.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >stream_encoding</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.stream.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >stream_filter_prepend</TD ></TR ></TABLE ></DIV ></BODY ></HTML >