<!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 >= 4.3.0, PHP 5)</P >proc_open -- 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 &pipes [, string cwd [, array env [, array other_options]]] )<BR ></BR ><P > <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 > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >cmd</CODE ></DT ><DD ><P > The command to execute </P ></DD ><DT ><CODE CLASS="parameter" >descriptorspec</CODE ></DT ><DD ><P > 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 > The currently supported pipe types are <TT CLASS="literal" >file</TT > and <TT CLASS="literal" >pipe</TT > . </P ><P > 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 > 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 > 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 > 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 > Allows you to specify additional options. Currently supported options include: <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD > <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 > <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 > <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 > <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 > 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 > <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 > 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 > 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 > 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 > <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"><?php<br />$descriptorspec </font><font color="#007700">= array(<br /> </font><font color="#0000BB">0 </font><font color="#007700">=> array(</font><font color="#DD0000">"pipe"</font><font color="#007700">, </font><font color="#DD0000">"r"</font><font color="#007700">), </font><font color="#FF8000">// stdin is a pipe that the child will read from<br /> </font><font color="#0000BB">1 </font><font color="#007700">=> array(</font><font color="#DD0000">"pipe"</font><font color="#007700">, </font><font color="#DD0000">"w"</font><font color="#007700">), </font><font color="#FF8000">// stdout is a pipe that the child will write to<br /> </font><font color="#0000BB">2 </font><font color="#007700">=> 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">=> </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 /> </font><font color="#FF8000">// $pipes now looks like this:<br /> // 0 => writeable handle connected to child stdin<br /> // 1 => readable handle connected to child stdout<br /> // Any error output will be appended to /tmp/error-output.txt<br /><br /> </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">'<?php print_r($_ENV); ?>'</font><font color="#007700">);<br /> </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 /> 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 /> </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 /> </font><font color="#FF8000">// It is important that you close any pipes before calling<br /> // proc_close in order to avoid a deadlock<br /> </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 /> 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">?></font> </font> </code></TD ></TR ></TABLE ><P >上例的输出类似于:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [some_option] => aeiou [PWD] => /tmp [SHLVL] => 1 [_] => /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 > <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 >