Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 9179

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>proc_open</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="Program Execution Functions"
HREF="ref.exec.html"><LINK
REL="PREVIOUS"
TITLE="proc_nice"
HREF="function.proc-nice.html"><LINK
REL="NEXT"
TITLE="proc_terminate"
HREF="function.proc-terminate.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.proc-nice.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.proc-terminate.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.proc-open"
></A
>proc_open</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN189207"
></A
><P
>    (PHP 4 &#62;= 4.3.0, PHP 5)</P
>proc_open&nbsp;--&nbsp;
     Execute a command and open file pointers for input/output
    </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN189210"
></A
><H2
>说明</H2
>resource <B
CLASS="methodname"
>proc_open</B
> ( string cmd, array descriptorspec, array &#38;pipes [, string cwd [, array env [, array other_options]]] )<BR
></BR
><P
>&#13;     <B
CLASS="function"
>proc_open()</B
> is similar to <A
HREF="function.popen.html"
><B
CLASS="function"
>popen()</B
></A
>
     but provides a much greater degree of control over the program execution.
    </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN189236"
></A
><H2
>参数</H2
><P
>&#13;   <P
></P
><DIV
CLASS="variablelist"
><DL
><DT
><CODE
CLASS="parameter"
>cmd</CODE
></DT
><DD
><P
>&#13;       The command to execute
      </P
></DD
><DT
><CODE
CLASS="parameter"
>descriptorspec</CODE
></DT
><DD
><P
>&#13;       An indexed array where the key represents the descriptor number and the
       value represents how PHP will pass that descriptor to the child
       process. 0 is stdin, 1 is stdout, while 2 is stderr.
      </P
><P
>&#13;       The currently supported pipe types are <TT
CLASS="literal"
>file</TT
> and
       <TT
CLASS="literal"
>pipe</TT
> .
      </P
><P
>&#13;       The file descriptor numbers are not limited to 0, 1 and 2 - you may
       specify any valid file descriptor number and it will be passed to the
       child process. This allows your script to interoperate with other
       scripts that run as "co-processes". In particular, this is useful for
       passing passphrases to programs like PGP, GPG and openssl in a more
       secure manner. It is also useful for reading status information
       provided by those programs on auxiliary file descriptors.
      </P
></DD
><DT
><CODE
CLASS="parameter"
>pipes</CODE
></DT
><DD
><P
>&#13;       Will be set to an indexed array of file pointers that correspond to
       PHP's end of any pipes that are created.
      </P
></DD
><DT
><CODE
CLASS="parameter"
>cwd</CODE
></DT
><DD
><P
>&#13;       The initial working dir for the command. This must be an
       <SPAN
CLASS="strong"
><B
CLASS="emphasis"
>absolute</B
></SPAN
> directory path, or <TT
CLASS="constant"
><B
>NULL</B
></TT
>
       if you want to use the default value (the working dir of the current
       PHP process)
      </P
></DD
><DT
><CODE
CLASS="parameter"
>env</CODE
></DT
><DD
><P
>&#13;       An array with the environment variables for the command that will be
       run, or <TT
CLASS="constant"
><B
>NULL</B
></TT
> to use the same environment as the current PHP process
      </P
></DD
><DT
><CODE
CLASS="parameter"
>other_options</CODE
></DT
><DD
><P
>&#13;       Allows you to specify additional options. Currently supported options
       include:
       <P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
>&#13;         <TT
CLASS="literal"
>suppress_errors</TT
> (windows only): suppresses errors
         generated by this function when it's set to <TT
CLASS="constant"
><B
>TRUE</B
></TT
>
        </TD
></TR
><TR
><TD
>&#13;         <TT
CLASS="literal"
>bypass_shell</TT
> (windows only): bypass
         <TT
CLASS="literal"
>cmd.exe</TT
> shell when set to <TT
CLASS="constant"
><B
>TRUE</B
></TT
>
        </TD
></TR
><TR
><TD
>&#13;         <TT
CLASS="literal"
>context</TT
>: stream context used when opening files
         (created with <A
HREF="function.stream-context-create.html"
><B
CLASS="function"
>stream_context_create()</B
></A
>)
        </TD
></TR
><TR
><TD
>&#13;         <TT
CLASS="literal"
>binary_pipes</TT
>: open pipes in binary mode, instead
         of using the usual <TT
CLASS="literal"
>stream_encoding</TT
>
        </TD
></TR
></TBODY
></TABLE
><P
></P
>
      </P
></DD
></DL
></DIV
>
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN189291"
></A
><H2
>返回值</H2
><P
>&#13;   Returns a resource representing the process, which should be freed using
   <A
HREF="function.proc-close.html"
><B
CLASS="function"
>proc_close()</B
></A
> when you are finished with it. On failure
   returns <TT
CLASS="constant"
><B
>FALSE</B
></TT
>.
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN189296"
></A
><H2
>更新日志</H2
><P
>&#13;   <DIV
CLASS="informaltable"
><P
></P
><A
NAME="AEN189299"
></A
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><THEAD
><TR
><TH
>版本</TH
><TH
>说明</TH
></TR
></THEAD
><TBODY
><TR
><TD
>6.0.0</TD
><TD
>&#13;        Added the <TT
CLASS="literal"
>context</TT
> and
        <TT
CLASS="literal"
>binary_pipes</TT
> options to the
        <CODE
CLASS="parameter"
>other_options</CODE
> parameter.
       </TD
></TR
><TR
><TD
>5.2.1</TD
><TD
>&#13;        Added the <TT
CLASS="literal"
>bypass_shell</TT
> option to the
        <CODE
CLASS="parameter"
>other_options</CODE
> parameter.
       </TD
></TR
><TR
><TD
>5.0.0</TD
><TD
>&#13;        Added the <CODE
CLASS="parameter"
>cwd</CODE
>, <CODE
CLASS="parameter"
>env</CODE
> and
        <CODE
CLASS="parameter"
>other_options</CODE
> parameters.
       </TD
></TR
></TBODY
></TABLE
><P
></P
></DIV
>
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN189323"
></A
><H2
>范例</H2
><P
>&#13;   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN189326"
></A
><P
><B
>例 1. A <B
CLASS="function"
>proc_open()</B
> example</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$descriptorspec </font><font color="#007700">= array(<br />&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">0 </font><font color="#007700">=&gt; array(</font><font color="#DD0000">"pipe"</font><font color="#007700">, </font><font color="#DD0000">"r"</font><font color="#007700">),&nbsp;&nbsp;</font><font color="#FF8000">// stdin is a pipe that the child will read from<br />&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">1 </font><font color="#007700">=&gt; array(</font><font color="#DD0000">"pipe"</font><font color="#007700">, </font><font color="#DD0000">"w"</font><font color="#007700">),&nbsp;&nbsp;</font><font color="#FF8000">// stdout is a pipe that the child will write to<br />&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">2 </font><font color="#007700">=&gt; array(</font><font color="#DD0000">"file"</font><font color="#007700">, </font><font color="#DD0000">"/tmp/error-output.txt"</font><font color="#007700">, </font><font color="#DD0000">"a"</font><font color="#007700">) </font><font color="#FF8000">// stderr is a file to write to<br /></font><font color="#007700">);<br /><br /></font><font color="#0000BB">$cwd </font><font color="#007700">= </font><font color="#DD0000">'/tmp'</font><font color="#007700">;<br /></font><font color="#0000BB">$env </font><font color="#007700">= array(</font><font color="#DD0000">'some_option' </font><font color="#007700">=&gt; </font><font color="#DD0000">'aeiou'</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$process </font><font color="#007700">= </font><font color="#0000BB">proc_open</font><font color="#007700">(</font><font color="#DD0000">'php'</font><font color="#007700">, </font><font color="#0000BB">$descriptorspec</font><font color="#007700">, </font><font color="#0000BB">$pipes</font><font color="#007700">, </font><font color="#0000BB">$cwd</font><font color="#007700">, </font><font color="#0000BB">$env</font><font color="#007700">);<br /><br />if (</font><font color="#0000BB">is_resource</font><font color="#007700">(</font><font color="#0000BB">$process</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// $pipes now looks like this:<br />&nbsp;&nbsp;&nbsp;&nbsp;// 0 =&gt; writeable handle connected to child stdin<br />&nbsp;&nbsp;&nbsp;&nbsp;// 1 =&gt; readable handle connected to child stdout<br />&nbsp;&nbsp;&nbsp;&nbsp;// Any error output will be appended to /tmp/error-output.txt<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$pipes</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">], </font><font color="#DD0000">'&lt;?php print_r($_ENV); ?&gt;'</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$pipes</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">stream_get_contents</font><font color="#007700">(</font><font color="#0000BB">$pipes</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$pipes</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// It is important that you close any pipes before calling<br />&nbsp;&nbsp;&nbsp;&nbsp;// proc_close in order to avoid a deadlock<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$return_value </font><font color="#007700">= </font><font color="#0000BB">proc_close</font><font color="#007700">(</font><font color="#0000BB">$process</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"command returned $return_value</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>上例的输出类似于:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Array
(
    [some_option] =&#62; aeiou
    [PWD] =&#62; /tmp
    [SHLVL] =&#62; 1
    [_] =&#62; /usr/local/bin/php
)
command returned 0</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN189332"
></A
><H2
>注释</H2
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
    Windows compatibility: Descriptors beyond 2 (stderr) are made available to
    the child process as inheritable handles, but since the Windows
    architecture does not associate file descriptor numbers with low-level
    handles, the child process does not (yet) have a means of accessing those
    handles. Stdin, stdout and stderr work as expected.
   </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
   If you only need a uni-directional (one-way) process pipe, use
   <A
HREF="function.popen.html"
><B
CLASS="function"
>popen()</B
></A
> instead, as it is much easier to use.
   </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN189339"
></A
><H2
>参见</H2
><P
>&#13;   <P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
><A
HREF="function.popen.html"
><B
CLASS="function"
>popen()</B
></A
></TD
></TR
><TR
><TD
><A
HREF="function.exec.html"
><B
CLASS="function"
>exec()</B
></A
></TD
></TR
><TR
><TD
><A
HREF="function.system.html"
><B
CLASS="function"
>system()</B
></A
></TD
></TR
><TR
><TD
><A
HREF="function.passthru.html"
><B
CLASS="function"
>passthru()</B
></A
></TD
></TR
><TR
><TD
><A
HREF="function.stream-select.html"
><B
CLASS="function"
>stream_select()</B
></A
></TD
></TR
><TR
><TD
>The <A
HREF="language.operators.execution.html"
>backtick operator</A
></TD
></TR
></TBODY
></TABLE
><P
></P
>
  </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.proc-nice.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.proc-terminate.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>proc_nice</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.exec.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>proc_terminate</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>