<HTML ><HEAD ><TITLE >proc_open</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Program Execution functions" HREF="ref.exec.html"><LINK REL="PREVIOUS" TITLE="proc_close" HREF="function.proc-close.html"><LINK REL="NEXT" TITLE="shell_exec" HREF="function.shell-exec.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></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 Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.proc-close.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.shell-exec.html" ACCESSKEY="N" >Next</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="AEN78857" ></A ><P > (PHP 4 >= 4.3.0)</P >proc_open -- Execute a command and open file pointers for input/output </DIV ><DIV CLASS="refsect1" ><A NAME="AEN78860" ></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. <TT CLASS="parameter" ><I >cmd</I ></TT > is the command to be executed by the shell. <TT CLASS="parameter" ><I >descriptorspec</I ></TT > 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. <TT CLASS="parameter" ><I >pipes</I ></TT > 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" ><A NAME="AEN78882" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("file", "/tmp/error-output.txt", "a"), // stderr is a file to write to ); $process = proc_open("php", $descriptorspec, $pipes); if (is_resource($process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // Any error output will be appended to /tmp/error-output.txt fwrite($pipes[0], "<?php echo \"Hello World!\"; ?>"); fclose($pipes[0]); while(!feof($pipes[1])) { echo fgets($pipes[1], 1024); } fclose($pipes[1]); // It is important that you close any pipes before calling // proc_close in order to avoid a deadlock $return_value = proc_close($process); echo "command returned $return_value\n"; }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > The file descriptor numbers in <TT CLASS="parameter" ><I >descriptorspec</I ></TT > 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 auxillary file descriptors. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </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 >Note: </B > This function was introduced in PHP 4.3.0. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </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.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-close.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.shell-exec.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >proc_close</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.exec.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >shell_exec</TD ></TR ></TABLE ></DIV ></BODY ></HTML >