function.stream-wrapper-register.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 486 行 · 第 1/3 页
HTML
486 行
<div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><b><a href="function.rmdir.html" class="function">rmdir</a></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$path</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$options</tt></span> )</div> <p class="para rdfs-comment"> This method is called in response to <a href="function.rmdir.html" class="function">rmdir()</a> calls on URL paths associated with the wrapper and should attempt to remove the directory specified by <i><tt class="parameter">path</tt></i>. It should return <b><tt>TRUE</tt></b> on success or <b><tt>FALSE</tt></b> on failure. In order for the appropriate error message to be returned, do not define this method if your wrapper does not support removing directories. Possible values for <i><tt class="parameter">options</tt></i> include <b><tt>STREAM_REPORT_ERRORS</tt></b>. </p> <blockquote><p><b class="note">Note</b>: <span class="simpara"> Userspace wrapper rmdir method is not supported prior to PHP 5.0.0. </span> </p></blockquote> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><b><b>dir_opendir</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$path</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$options</tt></span> )</div> <p class="para rdfs-comment"> This method is called immediately when your stream object is created for examining directory contents with <a href="function.opendir.html" class="function">opendir()</a>. <i><tt class="parameter">path</tt></i> specifies the URL that was passed to <a href="function.opendir.html" class="function">opendir()</a> and that this object is expected to explore. You can use <a href="function.parse-url.html" class="function">parse_url()</a> to break it apart. </p> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><b><b>url_stat</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$path</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$flags</tt></span> )</div> <p class="para rdfs-comment"> This method is called in response to <a href="function.stat.html" class="function">stat()</a> calls on the URL paths associated with the wrapper and should return as many elements in common with the system function as possible. Unknown or unavailable values should be set to a rational value (usually <b><tt>0</tt></b>). </p> <p class="para"> <i><tt class="parameter">flags</tt></i> holds additional flags set by the streams API. It can hold one or more of the following values OR'd together. <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Flag</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">STREAM_URL_STAT_LINK</td> <td colspan="1" rowspan="1" align="left"> For resources with the ability to link to other resource (such as an HTTP Location: forward, or a filesystem symlink). This flag specified that only information about the link itself should be returned, not the resource pointed to by the link. This flag is set in response to calls to <a href="function.lstat.html" class="function">lstat()</a>, <a href="function.is-link.html" class="function">is_link()</a>, or <a href="function.filetype.html" class="function">filetype()</a>. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">STREAM_URL_STAT_QUIET</td> <td colspan="1" rowspan="1" align="left">If this flag is set, your wrapper should not raise any errors. If this flag is not set, you are responsible for reporting errors using the <a href="function.trigger-error.html" class="function">trigger_error()</a> function during stating of the path. </td> </tr> </tbody> </colgroup> </table> </p> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><b><b>dir_readdir</b></b></span> ( <span class="methodparam">void</span> )</div> <p class="para rdfs-comment"> This method is called in response to <a href="function.readdir.html" class="function">readdir()</a> and should return a string representing the next filename in the location opened by <b>dir_opendir()</b>. </p> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><b><b>dir_rewinddir</b></b></span> ( <span class="methodparam">void</span> )</div> <p class="para rdfs-comment"> This method is called in response to <a href="function.rewinddir.html" class="function">rewinddir()</a> and should reset the output generated by <b>dir_readdir()</b>. i.e.: The next call to <b>dir_readdir()</b> should return the first entry in the location returned by <b>dir_opendir()</b>. </p> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><b><b>dir_closedir</b></b></span> ( <span class="methodparam">void</span> )</div> <p class="para rdfs-comment"> This method is called in response to <a href="function.closedir.html" class="function">closedir()</a>. You should release any resources which were locked or allocated during the opening and use of the directory stream. </p> <p class="para"> The example below implements a var:// protocol handler that allows read/write access to a named global variable using standard filesystem stream functions such as <a href="function.fread.html" class="function">fread()</a>. The var:// protocol implemented below, given the URL "var://foo" will read/write data to/from $GLOBALS["foo"]. <div class="example"> <p><b>Example #1 A Stream for reading/writing global variables</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">VariableStream </span><span style="color: #007700">{<br /> var </span><span style="color: #0000BB">$position</span><span style="color: #007700">;<br /> var </span><span style="color: #0000BB">$varname</span><span style="color: #007700">;<br /><br /> function </span><span style="color: #0000BB">stream_open</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">, </span><span style="color: #0000BB">$mode</span><span style="color: #007700">, </span><span style="color: #0000BB">$options</span><span style="color: #007700">, &</span><span style="color: #0000BB">$opened_path</span><span style="color: #007700">)<br /> {<br /> </span><span style="color: #0000BB">$url </span><span style="color: #007700">= </span><span style="color: #0000BB">parse_url</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">varname </span><span style="color: #007700">= </span><span style="color: #0000BB">$url</span><span style="color: #007700">[</span><span style="color: #DD0000">"host"</span><span style="color: #007700">];<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /><br /> return </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /> }<br /><br /> function </span><span style="color: #0000BB">stream_read</span><span style="color: #007700">(</span><span style="color: #0000BB">$count</span><span style="color: #007700">)<br /> {<br /> </span><span style="color: #0000BB">$ret </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">varname</span><span style="color: #007700">], </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position</span><span style="color: #007700">, </span><span style="color: #0000BB">$count</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position </span><span style="color: #007700">+= </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$ret</span><span style="color: #007700">);<br /> return </span><span style="color: #0000BB">$ret</span><span style="color: #007700">;<br /> }<br /><br /> function </span><span style="color: #0000BB">stream_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">)<br /> {<br /> </span><span style="color: #0000BB">$left </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">varname</span><span style="color: #007700">], </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$right </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">varname</span><span style="color: #007700">], </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position </span><span style="color: #007700">+ </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">));<br /> </span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">varname</span><span style="color: #007700">] = </span><span style="color: #0000BB">$left </span><span style="color: #007700">. </span><span style="color: #0000BB">$data </span><span style="color: #007700">. </span><span style="color: #0000BB">$right</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position </span><span style="color: #007700">+= </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /> return </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /> }<br /><br /> function </span><span style="color: #0000BB">stream_tell</span><span style="color: #007700">()<br /> {<br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position</span><span style="color: #007700">;<br /> }<br /><br /> function </span><span style="color: #0000BB">stream_eof</span><span style="color: #007700">()<br /> {<br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position </span><span style="color: #007700">>= </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">varname</span><span style="color: #007700">]);<br /> }<br /><br /> function </span><span style="color: #0000BB">stream_seek</span><span style="color: #007700">(</span><span style="color: #0000BB">$offset</span><span style="color: #007700">, </span><span style="color: #0000BB">$whence</span><span style="color: #007700">)<br /> {<br /> switch (</span><span style="color: #0000BB">$whence</span><span style="color: #007700">) {<br /> case </span><span style="color: #0000BB">SEEK_SET</span><span style="color: #007700">:<br /> if (</span><span style="color: #0000BB">$offset </span><span style="color: #007700">< </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">varname</span><span style="color: #007700">]) && </span><span style="color: #0000BB">$offset </span><span style="color: #007700">>= </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position </span><span style="color: #007700">= </span><span style="color: #0000BB">$offset</span><span style="color: #007700">;<br /> return </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /> } else {<br /> return </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /> }<br /> break;<br /><br /> case </span><span style="color: #0000BB">SEEK_CUR</span><span style="color: #007700">:<br /> if (</span><span style="color: #0000BB">$offset </span><span style="color: #007700">>= </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position </span><span style="color: #007700">+= </span><span style="color: #0000BB">$offset</span><span style="color: #007700">;<br /> return </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /> } else {<br /> return </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /> }<br /> break;<br /><br /> case </span><span style="color: #0000BB">SEEK_END</span><span style="color: #007700">:<br /> if (</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">varname</span><span style="color: #007700">]) + </span><span style="color: #0000BB">$offset </span><span style="color: #007700">>= </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position </span><span style="color: #007700">= </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">varname</span><span style="color: #007700">]) + </span><span style="color: #0000BB">$offset</span><span style="color: #007700">;<br /> return </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /> } else {<br /> return </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /> }<br /> break;<br /><br /> default:<br /> return </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /> }<br /> }<br />}<br /><br /></span><span style="color: #0000BB">stream_wrapper_register</span><span style="color: #007700">(</span><span style="color: #DD0000">"var"</span><span style="color: #007700">, </span><span style="color: #DD0000">"VariableStream"</span><span style="color: #007700">)<br /> or die(</span><span style="color: #DD0000">"Failed to register protocol"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$myvar </span><span style="color: #007700">= </span><span style="color: #DD0000">""</span><span style="color: #007700">;<br /><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">"var://myvar"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r+"</span><span style="color: #007700">);<br /><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">"line1\n"</span><span style="color: #007700">);<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">"line2\n"</span><span style="color: #007700">);<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">"line3\n"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">rewind</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />while (!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #0000BB">fgets</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 /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$myvar</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.stream-socket-shutdown.html">stream_socket_shutdown</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.stream-wrapper-restore.html">stream_wrapper_restore</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 + =
减小字号Ctrl + -
显示快捷键?