<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >proc_open</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual 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=ISO-8859-2"></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" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.proc-nice.html" ACCESSKEY="P" >Înapoi</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" >Înainte</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="AEN97058" ></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="AEN97061" ></A ><H2 >Description</H2 >resource <B CLASS="methodname" >proc_open</B > ( string cmd, array descriptorspec, array pipes)<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. <VAR CLASS="parameter" >cmd</VAR > is the command to be executed by the shell. <VAR CLASS="parameter" >descriptorspec</VAR > is an indexed array where the key represents the descriptor number and the value represents how PHP will pass that descriptor to the child process. <VAR CLASS="parameter" >pipes</VAR > will be set to an indexed array of file pointers that correspond to PHP's end of any pipes that are created. The return value is a resource representing the process; you should free it using <A HREF="function.proc-close.html" ><B CLASS="function" >proc_close()</B ></A > when you are finished with it. </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN97083" ></A ><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 /></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">);<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 echo \"Hello World!\"; ?>"</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 /> while (!</font><font color="#0000BB">feof</font><font color="#007700">(</font><font color="#0000BB">$pipes</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">])) {<br /> echo </font><font color="#0000BB">fgets</font><font color="#007700">(</font><font color="#0000BB">$pipes</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">], </font><font color="#0000BB">1024</font><font color="#007700">);<br /> }<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 /> </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 ></DIV > </P ><P > PHP 5RC2 introduces pty support for systems with Unix98 ptys. This allows your script to interact with applications that expect to be talking to a terminal. A pty works like a pipe, but is bi-directional, so there is no need to specify a read/write mode. The example below shows how to use a pty; note that you don't have to have all descriptors talking to a pty. Also note that only one pty is created, even though pty is specified 3 times. In a future version of PHP, it might be possible to do more than just read and write to the pty. </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN97087" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// Create a pseudo terminal for the child process<br /></font><font color="#0000BB">$descriptorspec </font><font color="#007700">= array(<br /> </font><font color="#0000BB">0 </font><font color="#007700">=> array(</font><font color="#DD0000">"pty"</font><font color="#007700">),<br /> </font><font color="#0000BB">1 </font><font color="#007700">=> array(</font><font color="#DD0000">"pty"</font><font color="#007700">),<br /> </font><font color="#0000BB">2 </font><font color="#007700">=> array(</font><font color="#DD0000">"pty"</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">"cvs -d:pserver:cvsread@cvs.php.net:/repository login"</font><font color="#007700">, </font><font color="#0000BB">$descriptorspec</font><font color="#007700">, </font><font color="#0000BB">$pipes</font><font color="#007700">);<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">// work with it here<br /></font><font color="#007700">}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > The file descriptor numbers in <VAR CLASS="parameter" >descriptorspec</VAR > 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 ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </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 >Notã: </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 ><P > See also <A HREF="function.stream-select.html" ><B CLASS="function" >stream_select()</B ></A >, <A HREF="function.exec.html" ><B CLASS="function" >exec()</B ></A >, <A HREF="function.system.html" ><B CLASS="function" >system()</B ></A >, <A HREF="function.passthru.html" ><B CLASS="function" >passthru()</B ></A >, <A HREF="function.popen.html" ><B CLASS="function" >popen()</B ></A >, <A HREF="function.escapeshellcmd.html" ><B CLASS="function" >escapeshellcmd()</B ></A >, and the <A HREF="language.operators.execution.html" >backtick operator</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.proc-nice.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.proc-terminate.html" ACCESSKEY="N" >Înainte</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" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >proc_terminate</TD ></TR ></TABLE ></DIV ></BODY ></HTML >